Question : Delete worksheets in workbook

I receive a workbook from a group,  which has several worksheetes that  I need to be able to sytematically delete certain worksheets, including hidden worksheets prior to running a query.

For example

Worksheet A Delete
Worksheet B keep
Worksheet C Keep
Worksheet D Delete
Hidden Worksheet E Delete
Hidden Worksheet F Delete
Worksheet G Keep

Then save the workbook that has Worksheet B, Worksheet C, and Worksheet G to a new file.

Answer : Delete worksheets in workbook

This code will save as "newfile.xls"  to the same directory with only the sheets that have names stored in strNames

Cheers

Dave
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
Sub RemoveSheets()
    Dim strNames(1 To 4) As String
    Dim ws As Worksheet

    ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\newfile"

    strNames(1) = "Sheet A"
    strNames(2) = "Sheet B"
  strNames(3) = "Sheet C"
  strNames(4) = "Sheet G"

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With

    On Error Resume Next
    For Each ws In ActiveWorkbook.Sheets
        If Not Application.WorksheetFunction.Match(ws.Name, strNames, 0) > 0 Then ws.Delete
    Next

    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With


End Sub
Random Solutions  
 
programming4us programming4us