what's wrong with this code?

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,894
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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
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

Nimrod

MrExcel MVP
Joined
Apr 29, 2002
Messages
6,259
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,195,635
Messages
6,010,823
Members
441,569
Latest member
PeggyLee

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
Top