VBA How to save workbook with a new name in the same file as original?

julierae

New Member
Joined
Jul 12, 2018
Messages
2
After running the macro, I want to save a new copied workbook with a filename using several cells in the worksheet + date. This file will be used by multiple people so the path will be different by user. I was hoping to rename the workbook and when saved, it would be in the same folder as the original workbook. I tried:

ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Copy

ThisWorkbook.SaveCopyAs Sheets("Sheet2").Range("A21") & " " & Sheets("Sheet2").Range("A18") & " " & Format(Now(), "DD-MMM-YYYY") & ".xlsx"

However, it is not saving it in the same folder. It saves in a random folder with the correct name. Also, the copied version of the workbook is open but does not reflect the new name. Is there a way to have the new file save in the same folder as the original file and to have the new opened document reflect the new name?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
julierae,

Welcome to the Board.

You might consider the following...

Code:
Sub SaveAs_1061438()
Dim fPath As String, fName As String

With ThisWorkbook
    fPath = .Path
    fName = .Sheets("Sheet2").Range("A21") & " " & .Sheets("Sheet2").Range("A18") & " " & Format(Now(), "DD-MMM-YYYY") & ".xlsx"
    .Sheets(Array("Sheet1", "Sheet2")).Copy
End With
ActiveWorkbook.SaveAs FileName:=fPath & "\" & fName
End Sub

The code captures the workbook path with the .Path property, then uses that to save the new/copied file.

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,214,381
Messages
6,119,192
Members
448,874
Latest member
Lancelots

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