在Excel里快速插入目录

来源:互联网 发布:网络扑克制作 编辑:程序博客网 时间:2024/06/05 06:57

在Excel里快速插入目录

标签(空格分隔): excel 工具


EXCEL本身没有Word那样插入目录的功能,不过我们可以通过VBA来实现类似功能,步骤如下:

1. 首先打开要创建目录的Excel文件

2. Alt+F11—>插入—>模块—>把下面的代码拷贝到模块中:

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 = “目录” Then
Sheets(“目录”).Move Before:=Sheets(1)
End If
Next i
If Sheets(1).Name <> “目录” Then
ShtCount = ShtCount + 1
Sheets(1).Select
Sheets.Add
Sheets(1).Name = “目录”
End If
Sheets(“目录”).Select
Columns(“B:B”).Delete Shift:=xlToLeft
Application.StatusBar = “正在生成目录…………请等待!”
For i = 2 To ShtCount
ActiveSheet.Hyperlinks.Add Anchor:=Worksheets(“目录”).Cells(i, 2), Address:=”“, SubAddress:= _
Sheets(i).Name & “!R1C1”, TextToDisplay:=Sheets(i).Name
Next
Sheets(“目录”).Select
Columns(“B:B”).AutoFit
Cells(1, 2) = “目录”
Set SelectionCell = Worksheets(“目录”).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

3.关闭VBA编辑界面

4.在你要插入的电子表格里按ALT+F8快捷键,选好你刚才写的模块mulu

5. 执行,OK,目录建好了。

6. 有图有真相

填入代码
插入目录
最终效果图

0 0
原创粉丝点击