Consulting

Results 1 to 13 of 13

Thread: Reset Excel sheet count?

  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.

  2. #2
    VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Is this what you want?


     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
        ActiveSheet.Name = "Sheet1"
        Application.DisplayAlerts = True
    End Sub
    Last edited by Aussiebear; 06-23-2025 at 01:24 PM.
    Peace of mind is found in some of the strangest places.

  3. #3
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    Thanks for the reply austenr, and my apologies for not being clear!

    If I am not mistaken, that will set the undeleted sheet to be named "Sheet1". I dont want to change the name of the remaining sheet, but do want all future sheets that are added to start at the lowest possible number. right now, it picks up where it left off. if Excel is restarted, it goes back to using the lowest possible number - i would like to, if possible, reset that counter without restarting excel!

    I am constantly amazed at how many settings there are, and I am pretty sure it is available somewhere!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Hi both,
    Can you please use line breaks in the code, or I'll need to get a second screen!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    I was going to add line breaks, but someone has beat me to it!

    I'll also work on creating a file to upload...

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Yeah.
    I did that.
    I see your problem, but I don't know if there is a "real" solution, but here's a possible workaround. It works with one error, but not two. Add the code to ThisWorkbook module.
    Private Sub Workbook_NewSheet(ByVal Sh As Object)
        On Error GoTo errh
        Sh.Name = "Sheet" & Sheets.Count
        Exit Sub
        errh:
        Sh.Name = "Sheet" & Sheets.Count + 1
    End Sub
    Last edited by Aussiebear; 06-23-2025 at 01:25 PM.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location

    Here's a sheet to play with

    I just saw your post, and will play with that soon. here is a demo file to tinker with...i need to take a break, but will check in again tomorrow...

    THANKS!

    PS - noticed the sheets.count property is read only...that may be a problem!

  8. #8
    VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Personally I never leave a sheet named "Sheet1" anyway - I prefer to use a name that tells me what's on the sheet!

  9. #9
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location

    Not ideal, but...

    attached is a solution i can live with; i was hoping to not have to go through all of my code again, but i can use search and replace to use the addsheets function instead of worksheets.add.

    i'll mark as solved, but if anyone comes up with a more elegant solution, i'm all ears...

  10. #10
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Option Explicit
     
    Sub DeleteSheets()
        Dim Answer As String, Sheet As Worksheet
        Answer = MsgBox("This will delete all sheets except for the sheet named '" & _
        ActiveSheet.Name & "'" & vbNewLine & vbNewLine & _
        "Click Yes to irreversibly erase the other sheets, or No to cancel " & _
        "this procedure", vbYesNo, "Warning - This Procedure Can Not Be Undone!")
        If Answer = vbNo Then
            Exit Sub
        Else
            Application.DisplayAlerts = False
            For Each Sheet In Worksheets
                If Sheet.Name <> ActiveSheet.Name Then Sheet.Delete
            Next
            Application.DisplayAlerts = True
        End If
    End Sub
     
    Sub AddSheets()
        Dim N As Long
        Worksheets.Add after:=Sheets(Sheets.Count)
        ActiveSheet.Name = "EZA" & Sheets.Count - 1
        For N = 1 To Sheets.Count
            ThisWorkbook.VBProject.VBComponents(Sheets(N).CodeName).Name = "Sheet" & N
        Next
    End Sub
    FYI, although it's possible to use the "+" symbol for concatenation, it is neither good nor accepted practice. It's best to reserve this symbol for additions only and to use the "&" symbol for concatenation. Here is what's said in the VBA Help files regarding this

    Quote Originally Posted by VBA Help files
    Remarks

    When you use the + operator, you may not be able to determine whether addition or string concatenation will occur. Use the & operator for concatenation to eliminate ambiguity and provide self-documenting code.
    Also, for new lines it's better practice to use vbNewLine (or even vbCr, or vbLf, or vbCrLf) rather than Chr(13)
    Last edited by Aussiebear; 06-23-2025 at 01:27 PM.
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Quote Originally Posted by johnske
    Also, for new lines it's better practice to use vbNewLine (or even vbCr, or vbLf, or vbCrLf) rather than Chr(13)
    vbNewLine is best. Macs recognise vbNewLine.
    Last edited by Bob Phillips; 12-22-2005 at 11:50 AM.

  12. #12
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    THANKS - that works quite nicely! regarding my bad coding habits, I have a bunch...thanks for pointing them out as it helps me develop new and better ones!

    Regarding chr(13), I looked all over to find out how to add a blank line in messageboxes and found that. It also works on Macs, but is certainly not intuitive! thanks again!

  13. #13
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by tpoynton
    THANKS - that works quite nicely! regarding my bad coding habits, I have a bunch...thanks for pointing them out as it helps me develop new and better ones!

    Regarding chr(13), I looked all over to find out how to add a blank line in messageboxes and found that. It also works on Macs, but is certainly not intuitive! thanks again!
    Not a prob, we're all here to learn. Happy holidays!
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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