[英]SaveAs automatically turns on AutoSave in Excel VBA
我有一個大文件,每天下載價格,然后以新名稱保存該文件的備份副本,並附上日期后綴。 只想將它保存在本地驅動器上,而不是讓它自動上傳到 Sharepoint。 這是出於兩個原因 (1) 速度 - 有時網絡連接速度很慢,保存到 Sharepoint 會降低性能;(2)“上傳到 Sharepoint”對話框似乎掛起,即使文件已明確上傳。
即使我關閉了自動保存,當 .SaveAs 代碼運行時它似乎會自動重新打開。 這是因為選擇了文件格式嗎? 我通常使用 .xlsb 來減小文件大小。
這是我正在使用的代碼:
Dim OrigName As String
Dim FilePath As String
Dim NewName As String
Dim DateSuffix As String
If ActiveWorkbook.AutoSaveOn = True Then
ActiveWorkbook.AutoSaveOn = False
Application.AutoRecover.Enabled = False
End If
SaveStart = Timer
Sheets("Parameters").Activate
RptDt = Range("End_Date").Offset(0, 1)
DateSuffix = Format(RptDt, "yyyymmdd") 'Year(RptDt) & Month(RptDt) & Day(RptDt)
Path = ActiveWorkbook.Path
OrigName = ActiveWorkbook.Name
OrigName = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1))
NewName = OrigName & " " & DateSuffix
If Right(ActiveWorkbook.Name, 4) = "xlsb" Then
NewName = Path & "\" & NewName & ".xlsb"
OrigName = Path & "\" & OrigName & ".xlsb"
Else
NewName = Path & "\" & NewName & ".xlsm"
OrigName = Path & "\" & OrigName & ".xlsm"
End If
Application.Calculation = xlCalculationManual
With ActiveWorkbook
If Right(.Name, 4) = "xlsb" Then
Application.DisplayAlerts = False
.SaveAs NewName, FileFormat:=xlExcel12
Application.DisplayAlerts = True
BeforeSave2 = Timer
Application.DisplayAlerts = False
.SaveAs OrigName, FileFormat:=xlExcel12
Application.DisplayAlerts = True
Else
Application.DisplayAlerts = False
.SaveAs NewName, FileFormat:=52
Application.DisplayAlerts = True
Application.DisplayAlerts = False
.SaveAs OrigName, FileFormat:=52
Application.DisplayAlerts = True
End If
End With
Application.DisplayAlerts = True
ActiveWorkbook.AutoSaveOn = True
Application.AutoRecover.Enabled = True
這是使用 SaveCopyAs 修改后的代碼:
NewName = NewName & ".xlsb"
OrigName = OrigName & ".xlsb"
With ActiveWorkbook
If Right(.Name, 4) = "xlsb" Then
Application.DisplayAlerts = False
.SaveCopyAs NewName
.SaveCopyAs OrigName
Application.DisplayAlerts = True
Else
Application.DisplayAlerts = False
.SaveCopyAs NewName
.SaveCopyAs OrigName
Application.DisplayAlerts = True
End If
End With
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.