How to Save as based on values in cells if the name already exists

szakharov7723

Board Regular
Joined
Jun 22, 2018
Messages
85
Office Version
  1. 2019
Platform
  1. Windows
So surprisingly I didn't find such a simple addition to the save as macro that already exists. So I created it myself.
There is already a macro that does saving as based on value in cell, but when this file already exists it pops up an error if you don't replace the file
My objective was to allow saving it as 2nd file. (there is a reason for that).
So sharing it in case somebody faces the same problem and doesn't know VBA.

Code:
Option Explicit


Sub SaveAsExample()


Dim FName           As String
Dim FPath           As String
On Error GoTo Handler:
    FPath = Sheets("Sheet1").Range("A2").Text
    FName = Sheets("Sheet1").Range("A1").Text
    ThisWorkbook.SaveAs Filename:=FPath & "\" & FName
    Exit Sub
Handler:
    FPath = Sheets("Sheet1").Range("A2").Text
    FName = Sheets("Sheet1").Range("A1").Text
    ThisWorkbook.SaveAs Filename:=FPath & "\" & FName & "_2"


End Sub


Note I also don't know VBA, just learnt it a bit enough to create this one.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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