Copy and Rename Worksheet Based on Cell Reference - VBA troubleshooting question

annieexcel

New Member
Joined
Jul 25, 2018
Messages
22
Hi guys! I'm having some trouble with what I thought should be a pretty easy macro. I'm constantly working in a big workbook with many tabs. I want to be able to easily save a copy of the worksheet I am currently in and for the file to be named based off a cell reference. This code works when I'm in a workbook with only 1 tab but does not work in my workbook with many tabs. It does copy and create the file but the file just opens, and it doesn't save for me. Do you have any ideas? Any insight would be greatly appreciated. :biggrin:

Sub Make_And_Rename_Workbook()


Dim wksht As Worksheet
Set wksht = ActiveSheet


Dim path As String
path = "C:\Desktop"


wksht.Copy
ActiveWorkbook.SaveAs Filename:=path & wksht.Range("B3").Value & ".xlsx"
End Sub
 
I just had "filename" in B3 ... I changed B3 to "Johnson 7-23 Notes" (which is similar to what I'll actually be naming these files.) It saved a copy to my desktop of a file named "Excel BackupsJohnson 7-23 Notes"


Much closer to what I actually want! :D .. Just need to figure out how to get it to go exactly where I want and not include "Excel Backups" in the name.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Is 'Johnson 7-23 Notes' definitely all you have in the cell?

Is there any other code?

What happens if you create a folder in C:\ and save to there rather than the Desktop?
 
Upvote 0
Yes there are no spaces or other characters in B3.

There is no other code.

I changed the code to:

Sub Make_And_Rename_Workbook()Dim wksht As Worksheet
Dim path As String


Set wksht = ActiveSheet


path = "C:\Excel Backups"


wksht.Copy


ActiveWorkbook.SaveAs Filename:=path & wksht.Range("B3").Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook


End Sub

I made a File named Excel Backups in C:\ and ran the code.

I got this error similar to earlier: https://imgur.com/a/Chts2Kr
 
Upvote 0
The path needs to end in a '\', that's basically the only thing I changed in the original code.
Code:
Sub Make_And_Rename_Workbook()Dim wksht As Worksheet
Dim path As String


Set wksht = ActiveSheet


path = "C:\Excel Backups\"


wksht.Copy


ActiveWorkbook.SaveAs Filename:=path & wksht.Range("B3").Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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