Results 1 to 13 of 13

Thread: Reset Excel sheet count?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location

    Reset Excel sheet count?

    Greetings - i have the following bit of code to delete all but the visible sheet. it works fine, but I was wondering if there is a way to set the sheetcount back to 1 as part of this.

    Right now, if I have sheets named like "sheet1", "sheet2" etc all the way up to "sheet30" and I run this procedure, it starts again at number 31 after sheets 1-30 are deleted. is there a way to reset this back to the lowest possible number? restarting excel does this, but I am wondering if it can be done through VBA.

    Public Sub DeleteSheets()
        Dim currentSheet As String
        Dim varAnswer As String
        Dim wsSheet As Worksheet
        Dim strName As String
        currentSheet = ActiveSheet.Name
        varAnswer = MsgBox("This will delete all sheets except for the sheet named '" _
       + currentSheet + "'" + (Chr(13)) + (Chr(13)) + _
        "Click Yes to irreversibly erase the other sheets, or No to cancel this procedure", _
        vbYesNo, "Warning - This Procedure Can Not Be Undone!")
        If varAnswer = vbNo Then
            Exit Sub
        Else:
            Application.DisplayAlerts = False
            With ActiveSheet
                For Each wsSheet In Worksheets
                    strName = wsSheet.Name
                    If strName <> currentSheet Then
                        wsSheet.Delete
                    End If
                Next wsSheet
            End With
        end if
        Application.DisplayAlerts = True
    End Sub
    What happens is that i have other procedures which add new sheets through VBA using worksheets.add. It is not that big of a deal, but I am curious!
    Last edited by Aussiebear; 06-23-2025 at 01:22 PM.

Posting Permissions

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