Copy Sheets - Not working

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, I am using the following code to copy multiple worksheets into a new workbook. Unfortunately, the worksheets aren't copying over. I am wondering if creating the new workbook is changing my "Activeworkbook" which is referenced on line 6. Any thoughts on how to correct this?

Also, I am hoping to delete these worksheets from my initial workbook after they have been copied into the new worksheet.

Code:
Sub Export_BOE_Sheets()
    Dim Sh As Worksheet, wb As Workbook, txt As String
    Set wb = Workbooks.Add
     wb.Sheets(1).Name = "BOE Cover Sheet"
        On Error Resume Next
            For Each Sh In ActiveWorkbook.Sheets
                If Left(Sh.Name, 9) = "Labor BOE" Then
                    Sh.Copy After:=wb.Sheets(wb.Sheets.Count)
                End If
            Next Sh
        On Error GoTo 0
    txt = InputBox("Enter the Date and/or Version number", "BASIS OF ESTIMATES")
    wb.SaveAs ThisWorkbook.Sheets("Home").Range("$F$9").Value & "_" & txt & ".xlsx"
    MsgBox "The BOE Export file generation is complete."
End Sub
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
When you add a new workbook it will became the ActiveWorkbook
before you add it you should create a set variable as the ActiveWorkbook and only then add a new one so you can activate the workbook you need
For instance
Code:
Dim aWb As Workbook
Dim wb As Workbook


Set aWb = ActiveWorkbook
Set wb = Workbook.Add


aWb.Activate
'your code


End Sub

if you will delete the initial worksheets why not to move then instead of copy

also, not sure if your error handler will work as you intended
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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