File save as - macro exit pulling a run-time error 1004

Deliverable7

New Member
Joined
Apr 9, 2016
Messages
33
Hi guys

I have a macro that pulls a file name and file path from cell within a sheet. The macro works well and when there is an existing file in the directory (with the same name) a dialog box pops up asking if I want to overwrite an existing file. If I select yes the macro works as intended. However when I select no to overwriting the existing file I get a Run-time error 1004:

Can someone please show me how to close the macro (prevent it from hanging) when selecting no to overwriting the existing file, a desired action would be to simply stop the macro.

[Option Explicit


Sub SaveShiftReport()

Dim FName As String
Dim FPath As String

FPath = Sheets("Day Shift").Range("AO1").Text
FName = Sheets("Day Shift").Range("AO2").Text
ThisWorkbook.SaveAs FileName:=FPath & "" & FName

On Error Resume Next

End Sub]

Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi,
try this

Code:
Sub SaveShiftReport()
    Dim FName As String, FPath As String
    
    With Sheets("Day Shift")
        FPath = .Range("AO1").Text
        FName = .Range("AO2").Text
    End With
    
    On Error GoTo exitsub
    ThisWorkbook.SaveAs Filename:=FPath & "" & FName, FileFormat:=52
    
exitsub:
    On Error GoTo 0
End Sub

this should ignore the error. You could include a MsgBox to report other errors if needed.

Dave
 
Upvote 0

Forum statistics

Threads
1,216,069
Messages
6,128,599
Members
449,460
Latest member
jgharbawi

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