VBA for SaveAs workbook

GuyGadois

Active Member
Joined
Jan 12, 2009
Messages
344
Office Version
  1. 2019
Platform
  1. Windows
I am using the following code to save a current workbook but I would like to modify it to save the current workbook as the new name and open that new workbook and close the original and leaving it untouched. Basically, using a Saveas instead. Currently it just saves the workbook as a new file and the user keeps working on the original workbook, not the saved one.

here is my code...

Code:
Sub SaveAsXLS()
     
    Dim FName           As String
    Dim FNumber         As String
    Dim fPath           As String
         
     FName = Range("Account_Name").Value
     
     fPath = Range("FilePath").Value
     
     ActiveWorkbook.SaveCopyAs fPath & FName & " - " & Format(Date, "YYYY.MM.DD") & ".xlsm"
     
     MsgBox "The File Has Been Saved as an Excel File" & vbNewLine & vbNewLine & "File Name: " & FName & vbNewLine & "Destination: " & fPath & vbNewLine


End Sub

Cheers

Guy
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Change SaveCopyAs to SaveAs.

Thanks for the response but when I change it to just read SaveAs I get an error that did not occur when I use SaveCopyAs

"Run-time Error '1004':
The file could not be accessed. Try one of the following:
Make sure the specified folder exists (it does)
Make sure the folder that contains the file is not read-only (it isn't)
Make sure it doesn't use <> ? [] : or * (It DOES use [ and ]. Why is this a problem with SaveAs and not SaveCopyAs)?
 
Upvote 0
I just verified that that is indeed the case, and it surprises me. So pick another name without brackets.
 
Upvote 0
I just verified that that is indeed the case, and it surprises me. So pick another name without brackets.

Or maybe I could just replace the "[" and "]" with ""

I tried this but it doesn't work. Any ideas how to replace both [ and ] with "" in VBA code?

Code:
FName = Replace(Range("Account_Name").Value, "]", "")(Replace(Range("Account_Name").Value, "[", ""))

Guy
 
Upvote 0
Code:
  FName = Replace(Replace(Range("Account_Name").Value, "[", ""), "]", "")
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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