The following code (blue font) finds specific worksheets in an active workbook, and copies them into a new workbook then names the new workbook the same name of the worksheet. It all works perfectly.
The second code below (red font) imports some vb into the new workbook. It works fine if I manually open the new workbook and run it in the immediate window. However, I would like to incorporate the red code into the blue code so it all runs as one. I've tried inserting the red line after the "ws.copy after" line and after the ".saveas" line, but no dice.
I think it is because I'm not making the new book active (but that is an uneducated guess). Does anyone know what I'm doing wrong?
For Each ws In wb.Worksheets
If UCase(Left(ws.Name, 2)) = "CC" Then
Set NewBook = Workbooks.Add
With NewBook
.Title = ws.Name
ws.Copy After:=NewBook.Worksheets("Sheet3")
For Each ws2 In NewBook.Worksheets
If ws2.Name <> ws.Name Then
ws2.Delete
End If
Next
.SaveAs Filename:="C:\Users\lmcginle\Desktop\" & ws.Name
.Close SaveChanges:=True
End With
End If
Next
wb.Activate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Application.VBE.ActiveVBProject.VBComponents.Import ("T:\Finance Mgmt\Reporting\MacroExport.bas")
The second code below (red font) imports some vb into the new workbook. It works fine if I manually open the new workbook and run it in the immediate window. However, I would like to incorporate the red code into the blue code so it all runs as one. I've tried inserting the red line after the "ws.copy after" line and after the ".saveas" line, but no dice.
I think it is because I'm not making the new book active (but that is an uneducated guess). Does anyone know what I'm doing wrong?
For Each ws In wb.Worksheets
If UCase(Left(ws.Name, 2)) = "CC" Then
Set NewBook = Workbooks.Add
With NewBook
.Title = ws.Name
ws.Copy After:=NewBook.Worksheets("Sheet3")
For Each ws2 In NewBook.Worksheets
If ws2.Name <> ws.Name Then
ws2.Delete
End If
Next
.SaveAs Filename:="C:\Users\lmcginle\Desktop\" & ws.Name
.Close SaveChanges:=True
End With
End If
Next
wb.Activate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Application.VBE.ActiveVBProject.VBComponents.Import ("T:\Finance Mgmt\Reporting\MacroExport.bas")