2009-12-30 17 views
0

問候一個和所有 - 一個聖誕節難題,任何人仍然看着這個網站...這工程,但如果我決定取消過程(即不保存文件,並在這個階段停止進程)它不會損害文件,但下面的marco(filltolastrow2)仍然激活,我怎麼能阻止這種情況發生?我如何讓我的宏停止,如果我取消保存副本

Public Sub SaveaCopyIncomeSheet() 
    Dim file_name As Variant 
    file_name = Application.GetSaveAsFilename("Overdue Report - Draft", filefilter:="Excel Files(*.xls),*.xls") 
    If file_name <> False Then 
     ActiveWorkbook.SaveAs Filename:=file_name 
     MsgBox "File Saved!" 
    End If 
    filltolastrow2 
End Sub 

回答

2

你可能想

If file_name <> False Then 
    ActiveWorkbook.SaveAs Filename:=file_name 
    MsgBox "File Saved!" 
    filltolastrow2 
End If 
0

備選:

Public Sub SaveaCopyIncomeSheet() 
    Dim file_name As Variant 
    file_name = Application.GetSaveAsFilename("Overdue Report - Draft", filefilter:="Excel Files(*.xls),*.xls") 

    If file_name = False Then GoTo E_NoFileName 

    ActiveWorkbook.SaveAs Filename:=file_name 
    MsgBox "File Saved!" 
    filltolastrow2 

Exit Sub 
E_NoFileName: 
    MsgBox "File Not Saved" 
End Sub 
+0

這是或多或少的如何不使用 「轉到」 的一個例子。我很肯定你聽說過「If - Then - Else」;-) – 2009-12-30 12:47:22

+0

嗯,你是對的我會將它編碼爲你的解決方案(因此「Alternative:」),但我喜歡展示一些變體。該解決方案不是非法的,不會使代碼更難理解,並且很簡單,所以我叮叮噹噹地確定:) – marg 2009-12-30 13:03:08

相關問題