VBA: How do you move sheets to a new book just created

shane_aldrich

Board Regular
Joined
Oct 23, 2009
Messages
148
The sub below does a great job of creating a new book by moving a copy of the definitions tab to the new book, and it saves it just fine, but when I try to move other sheets, I get a run-time error.


Sub Sub_10_04_Agent_Raw_New_Workbook()
Application.DisplayAlerts = False
'*************************
'*** Declare Variables ***
'*************************
Dim t1 As Date
Dim sFilePath As String
Dim sFileName As String
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
'*********************
'*** Set Variables ***
'*********************
t1 = Now
sFilePath = ActiveWorkbook.Path
sFileName = "Agent_Raw"
Set wb1 = ThisWorkbook
Set ws1 = Sheets("Definitions")
Set ws2 = Sheets("Summary_Agent")
ws1.Copy
Set wb2 = ActiveWorkbook
'***************
'*** Execute ***
'***************
wb2.SaveAs (sFilePath & "/" & sFileName & "_" & Format(t1, "YYYY_MM_DD") & ".xls")
Set wb2 = ActiveWorkbook

wb1.Activate
ws2.Activate
ws2.Copy After:=wb2.Sheets(Sheets.Count) 'here is where I'm getting the error.
wb1.Activate
End Sub



Thank you!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,203,326
Messages
6,054,745
Members
444,748
Latest member
knowak87

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