Excel 自动根据Sheet名生成目录

打开你要创建,目录的表格按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 '生成序号列 For i = 2 To ShtCount Sheets("目录").Select Columns("B:B").AutoFit Cells(i, 1) = i - 1 Next Sheets("目录").Select Columns("B:B").AutoFit Cells(1, 1) = "序号" Cells(1, 2) = "目录" Set SelectionCell = Worksheets("目录").Range("A1: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 复制完成后按F5运行即可,如果后期对表名做了修改,只需要重新F5运行刷新下目录即可。 自己又追加了生成序号列的代码,呵呵,稍作改动啊! (注:感谢原作者的辛苦劳动!)
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值