VBA code to copy/paste worksheet to new workbook

traub86

New Member
Joined
Nov 10, 2017
Messages
26
Hi,

I am looking to copy a worksheet in workbook1 to a named workbook before all other tabs AND name that tab with today's date. Any help would be much appreciated. Thanks.

:confused::confused::confused:
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Make sure both workbooks are open & try this
Code:
Sub Copysht()

    Workbooks("[COLOR=#ff0000]Book1.xlsm[/COLOR]").Sheets("[COLOR=#0000ff]Sheet1[/COLOR]").Copy before:=Workbooks("[COLOR=#ff0000]Book2.xlsm[/COLOR]").Sheets(1)
    Workbooks("[COLOR=#ff0000]Book2.xlsm[/COLOR]").Sheets(1).Name = Format(Date, "DD-MM-YYYY")

End Sub
Change the workbook names in red & the sheet name in blue to match your names.
 
Upvote 0
I get an error that says "subscript out of range" and highlights the top line. Also is there a way I can save to workbook2 without it being open?
 
Upvote 0
Were both workbooks Open? & did you change the names of the workbooks & sheet?
As for
Also is there a way I can save to workbook2 without it being open?
Not to my knowledge.
 
Upvote 0
Yes they were both open. here is the code with the named workbooks and sheets:
Code:
Sub Copysht()
    Workbooks("FINAL_JUMP MANIFEST_FINAL.xlsm").Sheets("DZ LOG").Copy before:=Workbooks("DO NOT DELETE_MASTER DZ LOG.xlsm").Sheets(1)
    Workbooks("DO NOT DELETE_MASTER DZ LOG.xlsm").Sheets(1).Name = Format(Date, "DD-MM-YYYY")
End Sub
 
Upvote 0
The destination workbook "DO NOT DELETE_MASTER DZ LOG" is NOT macro enabled...that might be it. Let me switch that over to see if that works...
 
Upvote 0
That worked perfectly! If I wanted to delete some macro buttons in the destination workbook could I use a code for that? If so how?
 
Upvote 0
Yes they were both open. here is the code with the named workbooks and sheets:
Code:
Sub Copysht()
    Workbooks("FINAL_JUMP MANIFEST_FINAL.xlsm").Sheets("DZ LOG").Copy before:=Workbooks("DO NOT DELETE_MASTER DZ LOG.xlsm").Sheets(1)
    Workbooks("DO NOT DELETE_MASTER DZ LOG.xlsm").Sheets(1).Name = Format(Date, "DD-MM-YYYY")
End Sub

If I want the name of the new sheet to have the date and a custom name suck as "Chalk 1" would I just put .Name = Format (Date, "DD-MM-YYYY") + Format (Text, "Chalk 1")?
 
Upvote 0
As it's just text you don't need the 2nd format
Code:
.Name = Format(Date, "DD-MM-YYYY") + " Chalk 1"
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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