代码运行时没有出错但没有保存文件

时间:2015-09-16 15:15:22

标签: excel excel-vba vba

应该打开文件,另存为,复制值,再次另存为(最终文件名),然后删除第一个另存为。我用它来实现一个临时的.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

1 个答案:

答案 0 :(得分:0)

您要求使用Application.GetSaveAsFilename保存文件名,但是您没有使用它:

Sub test()

    Dim a As String
    a = Application.GetSaveAsFilename(FileFilter:="Excel Files, *.xls")  'Ask for a filename to save as.
    ThisWorkbook.SaveAs a 'Save the file

End Sub

这是一天的结束,所以没有添加所有内容 - 检查a的结果是否为FALSE或其他一些不可用的名称。 SaveAs下还提供了各种选项。