Prevent Error 1004 "Method 'Save As' of object' Workbook' failed"

ESmith3187

New Member
Joined
Mar 22, 2017
Messages
5
I am using an input box to allow a user to create a new file name. The code works well until the user returns to the process and errantly tries to re-create the same file name. When that occurs the Run-time error '1004" msg appears "Method 'Save As' of object'_Workbook' failed. Does anyone have any insight or prevention regarding this error?

The code is:

[CODE}
Sub XXCreateNewFileName()
Dim ThisMonth As String
Dim ThisYear As String
ThisMonth = Format(Date, "mmm")
ThisYear = Format(Date, "yyyy")
Application.DisplayAlerts = False
Dim FileName1 As String
Application.DisplayAlerts = False
FileName1 = InputBox("Please input filename", "Filename")
ChDir "E:\AAA\2017" & ThisMonth & "\ "
ActiveWorkbook.SaveAs Filename:="E:\AAA\2017" & ThisMonth & "\ " & FileName1 & "_XXX_YYY" & "_" & "ZZZ" & "_" & ThisMonth & "_" & ThisYear, FileFormat:=51, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False

MsgBox "The name change was successful"
Application.DisplayAlerts = True
End Sub
[/CODE]
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I can not edit my posts yet, but there is an extra "Application.DisplayAlerts=False" in the code that I must have accidently put in while posting. Not quite sure how I did that!
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,363
Members
449,097
Latest member
thnirmitha

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