首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Excel 自动生成目录

第一步:打开需要制作目录的Excel工作表。

第二步:直接按住组合键 ALT+F11 会弹出一个窗口。

第三步:在弹出窗口中选择菜单:插入-模块,将会弹出一个新窗口。

第四步:在新窗口中,将下面的代码粘贴进去。然后直接按下F5。

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("A:A").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

第五步:关闭所有弹出窗口,回到Excel表格上来。

去看看原来的表就有一个目录了。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190810A0D4VG00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券