简体   繁体   中英

Macro excel Hyperlink backing to Index and skipping creating a index hyperlink

I have a macro that redo the table of contents page generated by our BI tool. The reason I do this is I rename and resort all tabs in the worksheet. I have two issues I'm trying to solve:

1. I'm trying to skip a hyperlink being generated for the first sheet
2. I will like to add a hyperlink in location (I1) on every other sheet that creates a hyperlink back to the Table of contents.

Sub listsheets()
Dim i As Integer
i = 2
Sheets("TOC").Select
Range("C6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
For Each s In Sheets

ActiveCell.formula = "=Hyperlink(""i'" & s.Name & "'!A4"",""" & s.Name & """)"
ActiveCell.Offset(1, 0).Activate
i = i + 1
Next s

End Sub

What about something like this:

Sub listsheets()

Dim i As Long
Dim rng As Range

Sheets("TOC").Select
Range(Range("C6"), Range("C6").End(xlDown)).ClearContents
Range("C6").Select
For Each s In Sheets
If s.Name <> "TOC" Then
Selection.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    s.Name & "!" & ActiveCell.Address(0, 0), TextToDisplay:=s.Name
ActiveCell.Offset(1, 0).Activate
End If
Next s

Set rng = Range("C6")

For i = 1 To ActiveWorkbook.Sheets.Count

    If Sheets(i).Name <> "TOC" Then

        Sheets(i).Hyperlinks.Add Anchor:=Sheets(i).Range("I1"), Address:="", SubAddress:= _
        "TOC!" & rng.Address(0, 0), TextToDisplay:="TOC"

        Set rng = rng.Offset(1, 0)

    End If

Next i

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM