开发者

excel macro beforeclose event

开发者 https://www.devze.com 2023-03-06 10:23 出处:网络
I am using the following macro to convert text to numbers for all the values. Since linked server is loading the xls, the format got messed up.

I am using the following macro to convert text to numbers for all the values. Since linked server is loading the xls, the format got messed up. Columns are already formatted e.g, accounting, percentage, number....etc, but even though everything are saved as text.

So I decided to run the macro before closiong开发者_如何学Go the workbook. The macros goes like this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Cells.Select
  Range("D1").Activate
  Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 1).Copy
  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlPasteSpecialOperationAdd
  With Selection
    .VerticalAlignment = xlTop
    .WrapText = False
  End With
  ActiveWorkbook.SaveAs "test.xls"
  ActiveWorkbook.Close 
End Sub

But the thing is, it pops up window to ask if I wanna save the changes, How can I fix that.

Above macro takes little longer to execute!!!

I have fixed columns(till D1) but valiable rows.

Can somebody help me to figure this out.


But the thing is, it pops up window to ask if I wanna save the changes, How can I fix that.

Add in ActiveWorkbook.Saved = True after ActiveWorkbook.SaveAs "test.xls". There are a few other options here(involving suppressing the alerts, but that could affect any other open workbooks, too).

Select whatever column you want:

Sub ConvertToText()
   Dim xCell As Range
   Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Select 'does the first column
   For Each xCell In Selection
     xCell.Value = CDec(xCell.Value)
   Next xCell
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消