Consulting

Results 1 to 2 of 2

Thread: Delete Sheets

  1. #1
    VBAX Regular
    Joined
    Jan 2007
    Posts
    28
    Location

    Delete Sheets

    I want to delete the following sheets with a macro, but all do not always exist. I need to delete if it is there:

    Sheet names:

    Stratford
    Castleberry
    Spartanburg
    Greenville
    Durham
    Tulsa
    Lakeland


    Thanks!

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    [VBA]Option Explicit

    Public Sub Example()
    DeleteWorksheets ActiveWorkbook, "Stratford", "Castleberry", _
    "Spartanburg", "Greenville", "Durham", "Tulsa", "Lakeland"
    End Sub

    Public Sub DeleteWorksheets(ByRef parentWorkbook As Excel.Workbook, _
    ParamArray worksheetNames() As Variant)
    Const lngLwrBnd_c As Long = 0
    Dim lngCrntWrksht As Long
    For lngCrntWrksht = lngLwrBnd_c To UBound(worksheetNames)
    If WorksheetExists(parentWorkbook, worksheetNames(lngCrntWrksht)) Then
    parentWorkbook.Worksheets(worksheetNames(lngCrntWrksht)).Delete
    End If
    Next
    End Sub

    Private Function WorksheetExists(ByRef parentWorkbook As Excel.Workbook, _
    ByVal worksheetName As String _
    ) As Boolean
    On Error Resume Next
    WorksheetExists = Not parentWorkbook.Worksheets(worksheetName) Is Nothing
    End Function[/VBA]
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •