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
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Are you sure the workbook isn't being saved?

Have a look in C:\ for any Excel files with names starting with 'Desktop'.
 
Upvote 0
Not sure what's going on there but try this.
Code:
Sub Make_And_Rename_Workbook()
Dim wksht As Worksheet
Dim path As String

    Set wksht = ActiveSheet

    path = "C:\Desktop\"

    wksht.Copy

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

End Sub
 
Upvote 0
Try this.
Code:
Sub Make_And_Rename_Workbook()
Dim wksht As Worksheet
Dim path As String

    Set wksht = ActiveSheet

    path = "C:\Desktop\"

    wksht.Copy

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

End Sub
 
Upvote 0
Well this is interesting... I tried changing the code to

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


Set wksht = ActiveSheet


path = "C:\Users\Steph\Desktop\Excel Backups"


wksht.Copy


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


End Sub

It now made and saved the file to my desktop under the name "Excel Backupfilename" (filename is what I have in Cell B3)
 
Upvote 0
Do you have a valid filename in B3 on the sheet being copied?
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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