0
它應該打開文件,另存爲,複製值,另存爲(最終文件名),然後刪除第一個另存爲。我正在使用它來實現一個臨時的.xlsx文件。它起作用,在窗口出現時打開並保存,然後刪除Temp文件,但在刪除臨時文件之前不保存該文件。代碼運行出錯,但它不保存文件
代碼:
Sub PracticeMakesPerfect()
Dim wbMain As Workbook
Dim Alpha As Workbook
Dim Beta As Workbook
Dim sFile As String
Dim PurgeTemp
Application.DisplayAlerts = False
Set wbMain = Workbooks("Macro Tester.xlsm")
Set Alpha = Workbooks.Open("C:\Users\frfcomputer\Desktop\Test.xlsx")
ActiveWorkbook.SaveAs "C:\Users\frfcomputer\Desktop\test\Temp.xlsx"
Set Beta = Workbooks("Temp.xlsx")
wbMain.Sheets("Sheet1").Range("A1").Value = Beta.Sheets("Sheet1").Range("A1").Value
Application.DisplayAlerts = True
Application.GetSaveAsFilename
ActiveWorkbook.Close
'Source File Location
sFile = "C:\Users\frfcomputer\Desktop\test\" & "Temp.xlsx"
'Sets Object
Set PurgeTemp = CreateObject("Scripting.FileSystemObject")
'Checks File Exists or Not
If PurgeTemp.FileExists(sFile) Then
'If file exists, delete the file
PurgeTemp.DeleteFile sFile, True
MsgBox "Deleted The File Successfully", vbInformation, "Done!"
Else
'If file does not exists
MsgBox "Specified File Not Found", vbInformation, "Not Found!"
End If
End Sub
我沒有看到「關閉」文件# – findwindow
我加了'ActiveWorkbook.Close'。現在沒有錯誤,它會刪除臨時文件,但它不會保存最終文件 – Duraholiday
需要查看代碼才能看到發生了什麼,但是如果您不打算使用它們,爲什麼要設置書本?編輯:達倫也說了什麼。 – findwindow