Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Save as" CSV - cancelling

Status
Not open for further replies.

cycledick

Technical User
Oct 6, 2005
141
US
I'm not a VBA expert and get what I need from a combination of code I find on the Internet mixed with code created when recording macros. I'm stuck trying to figure something out. I have the code below that takes the active worksheet in a workbook, copies it to a new workbook, then opens a dialog box to save it as a CSV file. However, if I press the cancel button on the dialog box, it continues processing the code in the routine that calls this routine. I want it to just "End" if the user presses cancel. Any help is appreciated.

Code:
Sub SaveSheetAsCSV(Optional SheetToSave As Worksheet)
    If SheetToSave Is Nothing Then Set SheetToSave = ActiveSheet
    
    SheetToSave.Copy
    
    If Not Application.Dialogs(xlDialogSaveAs).Show(, 6) Then
        ActiveWorkbook.Close SaveChanges:=False
    End If
End Sub

 
Nevermind. I was trying to make it more difficult that necessary. Since my copied worksheet is always going to have a different name than the original (if saved anyways - if cancelled it doesn't matter), I just added some code in the original routine to check the name of the active worksheet. If it wasn't what I expect it to be, then I just skip the part that was messing it up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top