Results 1 to 3 of 3

Thread: [RESOLVED] Save as macro returns error when the user hits cancel on the save as box

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    4

    Resolved [RESOLVED] Save as macro returns error when the user hits cancel on the save as box

    I have the following piece of cod, which is designed to obtain a file name and then save the excel file with the name. It works great until the user hits cancel. It then over saves the current file with the name false or returns the debugger. I want it to simply end if the user hits cancel but cannot find a way to do that.

    Code:
    Sub SaveFile()
    ' If the locations of the cells containing the names changes please simply redefine the ranges attached to the variables
    Sheets("Info & Readings").Select
    
    UPSName = Range("A18")
    MtnceType = Range("AB7") 'maintaince type, Major or minor
    'Address:     'Broken up into Street number, street, city, and province
        StNumb = Range("J9")
        StName = Range("N9")
        City = Range("T9")
        Prov = Range("Y9")
    MtnceWorkDate = Range("T4")
    
    If (UPSName = 0) Or (MtnceType = 0) Or (MtnceWorkDate = 0) Then
            MsgBox ("Please fill in required fields first: UPS Identification Name, Mtnce. Type, Address, and Mtnce. Work Date. Thank You")
        Else
           If (StNumb = "") Or (StName = "") Or (City = "") Or (Prov = "") Then
                MsgBox ("Please fill in required fields first: UPS Identification Name, Mtnce. Type, Address, and Mtnce. Work Date. Thank You")
            Else
                NomFichier = UPSName & " " & MtnceType & " " & " " & StNumb & " " & StName & " " & City & " " & Prov & " " & Format(Range("T4").Value, " yyyy-mmm-dd") & " .xls"
                ActiveWorkbook.SaveAs Filename:=Application.GetSaveAsFilename(NomFichier, FileFilter:="Fichier (*.xls), *.xls")
            End If
        End If
    
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Save as macro returns error when the user hits cancel on the save as box

    ActiveWorkbook.SaveAs Filename:=Application.GetSaveAsFilename(NomFichier, FileFilter:="Fichier (*.xls), *.xls")
    you will need to break this into separate parts so you can test if the user pressed cancel

    Code:
    fname = Application.GetSaveAsFilename(NomFichier, FileFilter:="Fichier (*.xls), *.xls")
    if not fname = false then ActiveWorkbook.SaveAs Filename:= fname
    define fname as required
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    4

    Re: Save as macro returns error when the user hits cancel on the save as box

    Thank you, this worked great.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width