|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
把冻结命令从函数移到过程中去,
- Sub 创建目录()
- Dim sht As Worksheet
- If Not ShtExists("目录") Then
- Set sht = Sheets.Add(before:=Sheets(1))
- sht.Name = "目录"
- With ActiveWindow
- .SplitColumn = 0
- .SplitRow = 2
- .FreezePanes = True
- End With
- Else
- Set sht = Sheets("目录")
- sht.Activate
- With ActiveWindow
- .SplitColumn = 0
- .SplitRow = 2
- .FreezePanes = True
- End With
- End If
- sht.[A1] = "序号"
- sht.[B1] = "目录"
- sht.[2:10000].ClearContents
- 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") = "" Or Sheets(i).Range("A1") = "返回目录" Then
- Sheets(i).Hyperlinks.Add Anchor:=Sheets(i).Range("A1"), Address:="", SubAddress:= _
- "目录!B" & i, TextToDisplay:="返回目录"
- Else
- Sheets(i).Select
- Sheets(i).Columns(1).Select
- Selection.Insert Shift:=xlToRight
- Sheets(i).Hyperlinks.Add Anchor:=Sheets(i).Range("A1"), Address:="", SubAddress:= _
- "目录!B" & i, TextToDisplay:="返回目录"
- End If
- Next
- Range("A:A,B1").HorizontalAlignment = xlCenter 'A列B1水平对齐
- Range("A1:B1").Font.Bold = True '设置A1:B1单元格的字体为加粗
- ActiveSheet.Columns("b:b").AutoFit
- Range("A1:B" & Cells(Rows.Count, 1).End(xlUp).Row).Borders.LineStyle = xlContinuous '添加边框
- Range("B2:B200").Font.Underline = xlUnderlineStyleNone '去除超链接下划线
- End Sub
- Function ShtExists(shtname)
- '判断Sheet表是否存在
- On Error Resume Next
- Dim s
- Err.Clear
- s = Sheets(shtname & "").Name
- If Err.Number = 0 Then
- ShtExists = True
- Else
- ShtExists = False
- End If
- End Function
复制代码
|
|