Delete worksheets after copying to new workbook

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, can anyone please provide guidance on how to edit the following code to *delete* the worksheets beginning with "Labor BOE" from my initial/primary excel worksheet, after these sheets are copied into a new workbook?

Here is the code being utilized:
Code:
Sub Export_X()
    Dim Sh As Worksheet, wb As Workbook, txt As String
    Set wb = Workbooks.Add
        On Error Resume Next
            For Each Sh In ThisWorkbook.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 Version Number or Date", "Export")
    wb.SaveAs ThisWorkbook.Sheets("Home").Range("$F$9").Value & "_" & txt & ".xlsx"
    MsgBox "BOE generation is complete."
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,
try changing this line:

Code:
Sh.Copy After:=wb.Sheets(wb.Sheets.Count)

to this:

Code:
Sh.Move After:=wb.Sheets(wb.Sheets.Count)


and see if this does what you want.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
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