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!
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!