VBA .Saveas Error 1004 When No or Cancel

Tommy Random

New Member
Joined
Dec 12, 2009
Messages
22
Hi Team,

I am using the following code to save a file with a new name

Code:
Private Sub CommandButton1_Click()
CommandButton1.BackColor = 5950882
ActiveWorkbook.SaveAs (Range("d4").Value & ".xlsm")


End Sub

When there is no existing file with the same name, the file is saved as desired.

When there is an existing file with the same name, a popup arises notifying the user as follows:

A file called 'File Name Specified in cell d4.xlsm' already exists in this location. Do you want to replace it?

If the user selects "Yes", no problem - the file is overwritten.

If the user selects "No", the following popup arises notifying the user as follows:

Run-time error '1004':
Method 'SaveAs' of object'_Workbook' failed.

I would like to modify the above code so that if the user selects "No", the VBA code would terminate without any further popup notification to the user.
(I still want the user the option to overwrite an existing file. So I do not wish to preempt all error handling).

I tried inserting the following line into the above code, but it did not help:

If Filename = "False" Then Exit Sub



Any suggestions?

Thank you.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Can you show us the code that generates the pop-up if the user specifies a file name that already exists? It's hard to help w/o seeing all your code.
 
Last edited:
Upvote 0
Thank you JoeMo for your interest in this question.

The code given is the code I am using in its entirety.

If a file name already exists, Excel itself generates the popup message:

A file called 'File Name Specified in cell d4.xlsm' already exists in this location. Do you want to replace it? (Yes, No, and Cancel options are given.)

Up to the point where that message is generated, all is well as I wish the user to retain the option to decline overwriting a file.

When the user selects "No" (or "Cancel") Excel generates another popup message (which is the one I would like to eliminate or bypass).

 
Upvote 0
Hi Joe,

Cell D4 contains this formula: ="Assignments "&D1&" "&D2
Cells D1 and D2 contain text, a students first name in D1 and a students last name in D2.
So the value in Cell D4, for example, would be (text) "Assignments Tommy Random"

Thanks.
 
Last edited:
Upvote 0
Try putting Application.DisplayAlerts = False just before the SaveAs line.
 
Upvote 0
Hi Joe,

Thank you for taking an interest in my question.

This code solves the problem:



Rich (BB code):
 Private Sub CommandButton1_Click()CommandButton1.BackColor = 5950882

On Error Resume Next
ActiveWorkbook.SaveAs (Range("d4").Value & ".xlsm")
On Error GoTo 0


End Sub

I consider this question resolved.
 
Last edited:
Upvote 0
I wanted to edit my last post but couldn't remember how to do so.

I found that Application.DisplayAlerts = False
does not allow the user to choose whether or not to overwrite an existing file with the same name. Instead, the file is overwritten without warning.

Again. Thank you for your interest in my question.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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