0
我寫了一個代碼,它分配給一個按鈕。當按下按鈕時,它會創建一個新的工作簿並詢問用戶在哪裏保存新文件。VBA Excel - 當用戶取消GetSaveAsFilename彈出框時退出sub
我想確保如果用戶單擊取消,然後關閉新的工作簿並退出子。
我寫下如下,但我不知道如何編寫更好的代碼。我知道if
可以改進。
Option Explicit
Sub Create_a_new_workbook_and_save_it()
Dim xlPath As String
Workbooks.Add
Application.DisplayAlerts = False
xlPath = Application.GetSaveAsFilename(Title:="Select where you want to save your file") & "xlsm"
If xlPath = "Falsexlsm" Then
ActiveWorkbook.Close
Exit Sub
End If
ActiveWorkbook.SaveAs _
Filename:=xlPath, FileFormat:=52
Application.DisplayAlerts = True
End Sub
沒有什麼特別的毛病'If'聲明,因爲它是,但如果你不這樣做的'Workbooks.Add'直到**在'If'之後**,您可以避免在'If'內執行'ActiveWorkbook.Close'。 – YowE3K
@ YowE3K你是對的。謝謝:) – Marshall