what's wrong with this code?

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,912
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the following code:

Code:
Sub List_Ws()

Dim Ws As Worksheet

    Sheets.Add
    ActiveSheet.Name = "Index"
    With Range("A1")
        .Value = "Sheet Name"
        .Font.bold = True
    End With

For Each Ws In ActiveWorkbook.Worksheets

Sheets("Index").Range("A65536").Select
Selection.End(xlUp).Select
With ActiveCell
    .Offset(1, 0).Value = Ws.Name
    .Hyperlinks.Add Anchor:=Selection, Address:="", _
        SubAddress:="'" & Ws.Name & "'!A1", TextToDisplay:=Ws.Name
End With

Next Ws

End Sub

Basically this gives me an index of the sheet names in the active workbook. It worked fine until I tried hyperlinking the sheet names to their corresponding sheets. It's overriding the title "Sheet Name" in A1 with "Index". Any ideas why?

Thanks,
Jon
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Jon

Try this.
Code:
Sub List_Ws()

Dim Ws As Worksheet

    Sheets.Add
    ActiveSheet.Name = "Index"
    With Range("A1")
        .Value = "Sheet Name"
        .Font.Bold = True
    End With

For Each Ws In ActiveWorkbook.Worksheets

Sheets("Index").Range("A65536").Select
Selection.End(xlUp).Select
With ActiveCell.Offset(1, 0)
    .Value = Ws.Name
    .Hyperlinks.Add Anchor:=ActiveCell.Offset(1, 0), Address:="", _
        SubAddress:="'" & Ws.Name & "'!A1", TextToDisplay:=Ws.Name
End With

Next Ws

End Sub
Or this.
Code:
Sub List_Ws()

Dim Ws As Worksheet
Dim I As Long
    Sheets.Add
    ActiveSheet.Name = "Index"
    With Range("A1")
        .Value = "Sheet Name"
        .Font.Bold = True
    End With
    
    I = 2
    
    For Each Ws In ActiveWorkbook.Worksheets

        With Sheets("Index").Range("A" & I)
            .Value = Ws.Name
            .Hyperlinks.Add Anchor:=Sheets("Index").Range("A" & I), Address:="", _
        SubAddress:="'" & Ws.Name & "'!A1", TextToDisplay:=Ws.Name
        End With
        I = I + 1
    Next Ws

End Sub
 
Upvote 0
Code:
Sub List_WsVer2()
Dim Rw As Integer
Dim Ws As Worksheet

    Sheets.Add
    ActiveSheet.Name = "Index"
    With Range("A1")
        .Value = "Sheet Name"
        .Font.Bold = True
    End With
Rw = 1
For Each Ws In ActiveWorkbook.Worksheets
    If Not Ws.Name = ActiveSheet.Name Then
    Rw = Rw + 1
    
    With Cells(Rw, 1)
        .Value = Ws.Name
        .Hyperlinks.Add Anchor:=Cells(Rw, 1), Address:="", _
            SubAddress:="'" & Ws.Name & "'!A1", TextToDisplay:=Ws.Name
    End With
    End If
Next Ws

End Sub

BIG TIP... wien yourself off using "selects" .. :wink:
 
Upvote 0

Forum statistics

Threads
1,225,531
Messages
6,185,486
Members
453,297
Latest member
alvintranvcu123

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