vba runtime error save as, need to run next macro in series

nflami

New Member
Joined
Apr 16, 2014
Messages
25
Hello,

I have the below code with a simple save as command for my macro. The issue I run into is, if the file already exist and I choose "No" or "cancel" in the dialog box the code returns a runtime error since it doesn't know what to do next. I haven't had any luck with any methods of continuing my code using resume. I don't want to use a false command on the current dialog box since I want the user aware the file already exist. My goal is when this save as box pops up and the user choose no or cancel for it to skip to the next macro.

Code:
Sub SaveFile()
     
Dim dtDate As Date
dtDate = Date

Dim strFile As String
strFile = "FilePath\FileName" & Format(dtDate, "mmddyyyy") & ".xlsm"

ActiveWorkbook.SaveAs Filename:=strFile, FileFormat _
    :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
         
     Application.Run ("Macro")


Thanks!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
.
.

Try adapting something like this:

Code:
Sub SaveAs()

    Dim Ans As Byte
    
    If Dir(strFile) <> vbNullString Then
        Ans = MsgBox( _
            Prompt:="A file named '" & strFile & _
                "' already exists in this location. " & _
                "Do you want to replace it?", _
            Buttons:=vbInformation + vbYesNoCancel)
    Else
        Ans = vbYes
    End If
    
    If Ans = vbYes Then
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs _
            Filename:=strFile, _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled
        Application.DisplayAlerts = True
    End If

End Sub
 
Upvote 0
Solved Re: vba runtime error save as, need to run next macro in series

That did the trick! Thank you for the help, much appreciated!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top