VBA: Copy sheet and rename book

Eean

New Member
Joined
May 26, 2010
Messages
44
Hello again!

I'm trying to create a VBA code to copy the active sheet to a new book but I want to rename the new book after the sheet name. Then I'm going to email it.
I have a lot of these to do.
I have this code, but can't work out how to rename the book.

Code:
Sub Copy()
'
' Copy Macro
'
'
    ActiveSheet.Select
    ActiveSheet.Copy
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I'm trying to create a VBA code to copy the active sheet to a new book but I want to rename the new book after the sheet name.
I think this code should do that for you:
Code:
Sub MySheetCopy()


    Dim mySourceWB As Workbook
    Dim mySourceSheet As Worksheet
    Dim myDestWB As Workbook
    Dim myNewFileName As String
    
'   First capture current workbook and worksheet
    Set mySourceWB = ActiveWorkbook
    Set mySourceSheet = ActiveSheet


'   Build new file name based
    myNewFileName = mySourceWB.Path & "\" & mySourceSheet.Name & ".xlsx"


'   Add new workbook and save with name of sheet from other file
    Workbooks.Add
    ActiveWorkbook.SaveAs Filename:=myNewFileName
    Set myDestWB = ActiveWorkbook
    
'   Copy over sheet from previous file
    mySourceWB.Activate
    Cells.Copy
    myDestWB.Activate
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
'   Resave new workbook
    ActiveWorkbook.Save


End Sub
 
Upvote 0
That works brilliantly!
I think I can follow most of that code too.
Thanks so much.
 
Upvote 0
Your welcome!

Note some of the assumptions I made, namely:
- you wanted to save the new file to the same path as the original file
- you wanted to use a "xlsx" extension

Either of those things can be changed easily enough, if they don't suit your needs.
 
Upvote 0
Your welcome!

Note some of the assumptions I made, namely:
- you wanted to save the new file to the same path as the original file
- you wanted to use a "xlsx" extension

Either of those things can be changed easily enough, if they don't suit your needs.

Is it neccesary to define the path when you're defining a filename?
I think I may have been going wrong with that.
 
Upvote 0
I believe that you usually need to include the path as part of the file name when saving the file with the SaveAs command (or else it doesn't know where to save it to).
 
Upvote 0
I believe that you usually need to include the path as part of the file name when saving the file with the SaveAs command (or else it doesn't know where to save it to).

I don't think I factored in that it needed saving...
You live and learn!
Thanks for sharing your excellent knowledge!
 
Upvote 0
Does anyone know why I'm having a problem with this code when trying to save .xlsm format?

.xlsx works fine for me so I'm a bit miffed.


The only change to the code I made was from this:
Code:
myNewFileName = mySourceWB.Path & "\" & mySourceSheet.Name & ".xlsx"

To this:
Code:
myNewFileName = mySourceWB.Path & "\" & mySourceSheet.Name & ".xlsm"

it hags on the SaveAs line with:
"Run-time error '1004' This file ext cannot be used with the selected file type. Change the file ext et.
 
Upvote 0
The issue isn't with the line you highlighted, it is with the SaveAs line. Since it is not being saved to the "default" format, you cannot use the default settings and actually need to specify the File Format.
So change this:
Code:
    ActiveWorkbook.SaveAs Filename:=myNewFileName
to this:
Code:
    ActiveWorkbook.SaveAs Filename:=myNewFileName, _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

There is an easy way to figure this out. Simply turn on the Macro Recorder and record yourself saving a file in the "xlsm" format. Then stop the Recorder and view the code. This will show you all the arguments and settings you need.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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