Save file already exists

SamSlade

Board Regular
Joined
Feb 10, 2006
Messages
69
Hi

I feel I am missing something very obvious here, but I want to give a file a specific name and then save it in VBA. That part is no problem.

The difficulty arises when that file name already exists - under those circumstances I want to add a v2 or v3 etc after the file name. I have tried error trapping but it does not seem to work.

Is there a way to achieve this please?

Many thanks

Kind regards

Sam
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You could create a loop in which you check if the filename you want to use for saving already exists, if it does, change the filename.
Stay in that loop until you have a filename that doesn't exist yet, then exit the loop and save the file.

To check if a file already exists, you could use the following function:
Code:
Public Function FileExists(Filename As String) As Boolean
    On Error Resume Next
    FileExists = IIf(Dir(Filename) = "", False, True)
End Function
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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