Excel中的文件多了,就希望能生成一个目录,方便快速跳转,但是网上的代码参差不齐,无法很好的达到预期,所以小编刚刚先写了一个分享给大家!
▼ 动画-效果演示
▍功能说明:
点击执行代码,一键为当前Excel文件生成目录 目录自动调整格式,字体大小,自动对齐等 返回目录,在任意表中,点击名称框,选择返回目录,快速切换到目录表
'功能:一键生成目录及返回目录
'作者:E精精
2023-6-23 02:26:46 :
Sub CreateContents()
Dim ws As Worksheet
Dim i As Long
Dim contentsSht As Worksheet
Const cntShtName As String = "目录"
On Error Resume Next
'删除已存在的目录表
False =
ThisWorkbook.Worksheets(cntShtName).Delete
True =
'在开始位置插入一张目录表
Set contentsSht = ThisWorkbook.Sheets.Add( _
before:=ThisWorkbook.Sheets(1))
cntShtName =
With contentsSht
'添加目录
B1] = [{"序号","内容"}] :
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> .Name Then
i = i + 1
+ 1, 1).Value = i
_
Anchor:=.Cells(i + 1, 2), _
Address:="", _
SubAddress:="'" & ws.Name & "'!A1", _
TextToDisplay:=ws.Name
End If
Next ws
'格式设置
xlCenter =
xlLeft =
With .Range("A1").CurrentRegion
With .Font
False =
12 =
"微软雅黑" =
End With
True =
End With
'添加返回目录
.Names("返回目录").Delete
Name:="返回目录", _
RefersToR1C1:="=目录!R1C1"
End With
MsgBox "目录表已生成,请查看", vbInformation
End Sub
最后祝大家节日快乐吧!今天就先到这里!快去试试吧!现在没有时间的同学不要忘了收藏、转发方便备查,所谓“找东西不要命,找起东西要人命”!