简体   繁体   中英

List Excel Sheet & Chart Tabs

This lists & hyperlinks, all Sheets then all Charts. What I would like is for the list to be in the order they are found in the workbook; ie, sheet1, sheet2, chart1, sheet3, chart 2, etc. I have 126 tabs so a manual reorder isn't an option.

I'm sure there is a way to use an IF/OR but I have no idea how to do the THEN.

Sub CreateLinksToAll()
Dim sh As Worksheet
Dim ch As Chart

Application.ScreenUpdating = False
Sheets("Index").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A2").Select

   For Each sh In ActiveWorkbook.Worksheets
     If ActiveSheet.Name <> sh.Name And sh.Visible Then
       ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
       "'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name
       ActiveCell.Offset(1, 0).Select
    End If
   Next sh

   For Each ch In ActiveWorkbook.Charts
     If ActiveSheet.Name <> ch.Name And ch.Visible Then
       ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
       "'" & ch.Name & "'" & "!A1", TextToDisplay:=ch.Name
       ActiveCell.Offset(1, 0).Select
     End If
   Next ch

  Columns("A:A").Font.Size = 12
  Range("A1").Select
End Sub

You're getting all your worksheets first, and then the charts, because you're doing two iterations over each respective collection.

Instead, iterate over the Workbook's .Sheets collection.

Modified from my original answer : You dont' need any conditional test for Sheet/Chart in this approach. This is untested but I think it should work, let me know if it doesn't (probably missing a parenthesis somewhere...).

Sub Test()

Dim sh As Variant
Dim rng as Range
Dim r as Integer

Application.ScreenUpdating = False
Set rng = Sheets("Index").Range("A2", Range("A2").End(xlDown))
rng.ClearContents
Set rng = rng.Resize(1,1)

For Each sh In ActiveWorkbook.Sheets
   r = r + 1
   rng.Cells(r).Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
       "'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name

Next
Application.ScreenUpdating = True
End Sub

What I would like is for the list to be in the order they are found in the workbook; ie, sheet1, sheet2, chart1, sheet3, chart 2, etc

For this use a simple For Loop

For i = 1 To ThisWorkbook.Sheets.Count
    Debug.Print ThisWorkbook.Sheets(i).Name
Next

Here I am simply displaying the name of the sheets in the order they are present in the workbook. Simply put your hyperlink code in lieu of that.

EDIT

Also You may find THIS interesting :)

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