|
代码如下:
- Sub 创建目录()
- Dim sht As Worksheet
- If Not ShtExists("目录") Then
- Set sht = Sheets.Add(before:=Sheets(1))
- sht.Name = "目录"
- Else
- Set sht = Sheets("目录")
- End If
- With sht
- r = Sheets.Count
- .UsedRange.Offset(1).Clear
- .[a1] = "序号"
- .[b1] = "目录"
- With .[a1].Resize(r, 2)
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter '//列居中
- .VerticalAlignment = xlCenter
- End With
- .[b2].Resize(r, 1).HorizontalAlignment = xlLeft
- End With
- For i = 2 To Sheets.Count
- sht.Cells(i, 1) = i - 1
- sht.Cells(i, 2) = Sheets(i).Name
- '主表添加超链接
- sht.Hyperlinks.Add Anchor:=sht.Cells(i, 2), Address:="", SubAddress:= _
- "'" & Sheets(i).Name & "'!A1", TextToDisplay:=Sheets(i).Name
- '子表添加返回超链接
- If Sheets(i).Range("A1") = "" Then
- Sheets(i).Hyperlinks.Add Anchor:=Sheets(i).Range("A1"), Address:="", SubAddress:= _
- "目录!B" & i, TextToDisplay:="返回目录"
- Else
- Sheets(i).Hyperlinks.Add Anchor:=Sheets(i).Range("A1"), Address:="", SubAddress:= _
- "目录!B" & i
- End If
- Next
- End Sub
复制代码
|
|