views:

92

answers:

2

Greetings one and all - a christmas puzzle for anyone still looking at this site...This works but if i decide to cancel the process (ie not save a file and stop the process at this stage) it doesn't svae the file but the following marco (filltolastrow2) is still activated how can I stop this happening?

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  A: 

You possibly want

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

Alternative:

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
marg
That's more or less an example of how NOT to use "GoTo". I am pretty sure you heard of "If - Then - Else" ;-)
Doc Brown
Well you are right I would Code it like your Solution (hence the "Alternative:") but I like to show some variety. The Solution is not illegal, does not make the code harder to understand and is simple so I tink it's ok :)
marg