Excel Skill---Generate Table of Contents based on Sheet Name (根据Sheet名称产生目录)

Sub mulu()
On Error GoTo Tuichu
Dim i As Integer
Dim ShtCount As Integer
Dim SelectionCell As Range

ShtCount = Worksheets.Count
If ShtCount = 0 Or ShtCount = 1 Then Exit Sub
Application.ScreenUpdating = False
For i = 1 To ShtCount
If Sheets(i).Name = "Inventory" Then
Sheets("Inventory").Move Before:=Sheets(1)
End If
Next i
If Sheets(1).Name <> "Inventory" Then
ShtCount = ShtCount + 1
Sheets(1).Select
Sheets.Add
Sheets(1).Name = "Inventory"
End If
Sheets("Inventory").Select
Columns("B:B").Delete Shift:=xlToLeft
Application.StatusBar = "Generating Inventory…………WAITING!"
For i = 2 To ShtCount
ActiveSheet.Hyperlinks.Add Anchor:=Worksheets("Inventory").Cells(i, 2), Address:="", SubAddress:= _
"'" & Sheets(i).Name & "'!R1C1", TextToDisplay:=Sheets(i).Name
Next
Sheets("Inventory").Select
Columns("B:B").AutoFit
Cells(1, 2) = "Inventory"
Set SelectionCell = Worksheets("Inventory").Range("B1")
With SelectionCell
.HorizontalAlignment = xlDistributed
.VerticalAlignment = xlCenter
.AddIndent = True
.Font.Bold = True
.Interior.ColorIndex = 34
End With
Application.StatusBar = False
Application.ScreenUpdating = True
Tuichu:
End Sub

原文地址:https://www.cnblogs.com/lxxlovekang/p/4342265.html