VBA to Dynamically list Worksheet Names

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hi there,

I am looking for some VBA that can dynamically list all the names of the worksheets in a workbook (In Column A).
i.e. If I add a worksheet the name appears, if i edit the name the name is edited on the list and if I delete a worksheet the names is removed.

It doesn't need to be in order.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi, is there a way to edit this VBA to only show the list of worksheets between two specific worksheetssheets? For example, I have an excel file with 4 worksheets at the beginning that are different reports, then I have a worksheet named "Start", then there are about 40 worksheets with various names, then there is a worksheet named "End." I would like the list to update with only the worksheets that fall between "Start" and "End"

Any ideas?

Thank you!

Ed
Untested, but I think this revision will do what you want.
Code:
Private Sub Worksheet_Activate()
Dim wSheet As Worksheet
Dim n As Integer
Dim calcState As Long, scrUpdateState As Long

calcState = Application.Calculation
Application.Calculation = xlCalculationManual
scrUpdateState = Application.ScreenUpdating
Application.ScreenUpdating = False

n = 1

    With Me
        .Columns(1).ClearContents
        .Cells(1, 1) = "INDEX"
        .Cells(1, 1).Name = "Index"
    End With
    
    For Each wSheet In Worksheets
        If wSheet.Name <> Me.Name And wSheet.Index > Sheets("Start").Index And _
            wSheet.Index < Sheets("End").Index Then
            n = n + 1
                With wSheet
                    .Range("A1").Name = "Start_" & wSheet.Index
                     .Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
                    SubAddress:="Index", TextToDisplay:="Back to Index"
                End With
                
                Me.Hyperlinks.Add anchor:=Me.Cells(n, 1), Address:="", _
                SubAddress:="Start_" & wSheet.Index, TextToDisplay:=wSheet.Name
        End If
    Next wSheet
    
Application.Calculation = calcState
Application.ScreenUpdating = scrUpdateState
End Sub
 
Upvote 0
Wow, works perfectly! I don't know how you know how to do this stuff but it is amazing. Thank you so much!
 
Upvote 0
I am trying to adapt this code for a workbook I am working on. It works perfectly until I try to add my hidden template "Master" then I get a runtime error. Here is the code:

Code:
Private Sub Worksheet_Activate()Dim wSheet As Worksheet
Dim n As Integer
Dim calcState As Long, scrUpdateState As Long


calcState = Application.Calculation
Application.Calculation = xlCalculationManual
scrUpdateState = Application.ScreenUpdating
Application.ScreenUpdating = False


n = 1


    With Me
        .Columns(1).ClearContents
        .Cells(1, 1) = "COLLECTION"
        .Cells(1, 1).Name = "Collection"
    End With
    
    For Each wSheet In Worksheets
        If wSheet.Name <> Me.Name And wSheet.Visible = xlSheetVisible Then
            n = n + 1
                With wSheet
                    .Range("A1").Name = "Start_" & wSheet.Index
                    .Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
                    SubAddress:="Collection", TextToDisplay:="Back to Collection"
                End With
                
                Me.Hyperlinks.Add anchor:=Me.Cells(n, 1), Address:="", _
                SubAddress:="Start_" & wSheet.Index, TextToDisplay:=wSheet.Name
        End If
    Next wSheet
    
Application.Calculation = calcState
Application.ScreenUpdating = scrUpdateState
End Sub

This is the portion of the code that gets flagged when I go to debug.

Code:
.Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
                    SubAddress:="Collection", TextToDisplay:="Back to Collection"

Thanks for any help. I've been working on this for a week now.
 
Upvote 0
I am trying to adapt this code for a workbook I am working on. It works perfectly until I try to add my hidden template "Master" then I get a runtime error. Here is the code:

Code:
Private Sub Worksheet_Activate()Dim wSheet As Worksheet
Dim n As Integer
Dim calcState As Long, scrUpdateState As Long


calcState = Application.Calculation
Application.Calculation = xlCalculationManual
scrUpdateState = Application.ScreenUpdating
Application.ScreenUpdating = False


n = 1


    With Me
        .Columns(1).ClearContents
        .Cells(1, 1) = "COLLECTION"
        .Cells(1, 1).Name = "Collection"
    End With
    
    For Each wSheet In Worksheets
        If wSheet.Name <> Me.Name And [B][COLOR=#ff0000]wSheet.Visible = xlSheetVisible[/COLOR][/B] Then
            n = n + 1
                With wSheet
                    .Range("A1").Name = "Start_" & wSheet.Index
                    .Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
                    SubAddress:="Collection", TextToDisplay:="Back to Collection"
                End With
                
                Me.Hyperlinks.Add anchor:=Me.Cells(n, 1), Address:="", _
                SubAddress:="Start_" & wSheet.Index, TextToDisplay:=wSheet.Name
        End If
    Next wSheet
    
Application.Calculation = calcState
Application.ScreenUpdating = scrUpdateState
End Sub

This is the portion of the code that gets flagged when I go to debug.

Code:
.Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
                    SubAddress:="Collection", TextToDisplay:="Back to Collection"

Thanks for any help. I've been working on this for a week now.
Not sure I understand. is "Master" a worksheet in the workbook the code is in? If yes, and Master is hidden, I don't see how you ever get to the error line with the bit I highlighted in red there.
 
Upvote 0
Not sure I understand. is "Master" a worksheet in the workbook the code is in? If yes, and Master is hidden, I don't see how you ever get to the error line with the bit I highlighted in red there.


The "Master" is a hidden worksheet in the same workbook. I have a separate macro that unhides the MASTER, makes a copy of it, renames it to whatever I input then rehides it. I only want to have a list of the visible worksheets.


The worksheet order (if it matters) is, Master, Notes, Drop Down, Instructions, Collection, new sheet1, new sheet2, etc. The sheets in blue text are hidden.
 
Upvote 0
The "Master" is a hidden worksheet in the same workbook. I have a separate macro that unhides the MASTER, makes a copy of it, renames it to whatever I input then rehides it. I only want to have a list of the visible worksheets.


The worksheet order (if it matters) is, Master, Notes, Drop Down, Instructions, Collection, new sheet1, new sheet2, etc. The sheets in blue text are hidden.
With the code you posted the hidden sheets should not be listed. If you want them listed, then remove the bit I highlighted in red in post #17.
 
Upvote 0
With the code you posted the hidden sheets should not be listed. If you want them listed, then remove the bit I highlighted in red in post #17.

The hidden sheets aren't listed but neither are any of the other ones and I get a runtime error about the hyperlink range.
Code:
[COLOR=#333333][FONT=monospace][I].Hyperlinks.Add anchor:=.Range("A1"), Address:="", _SubAddress:="Collection", TextToDisplay:="Back to Collection"[/I][/FONT][/COLOR]

If I use the code in a blank workbook and move over the other sheets one by one everything works great until I add the Master sheet.
****** id="cke_pastebin" style="position: absolute; top: 0px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">.Hyperlinks.Add anchor:=.Range("A1"), Address:="", _ SubAddress:="Collection", TextToDisplay:="Back to Collection"</body>
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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