系統(tǒng):Windows 10
Part 1:為什么整這個?
Part 2:框架搭建
效果圖 Part 3:代碼部分Sub 各工作表名稱()
Rem>>\該程序會新建一個工作表“目錄”,并置于第一個工作表位置
Rem>>\獲得當前工作簿除“目錄”工作表以外的工作表名稱
Rem>>\工作表名稱存放于“目錄”工作表C列,從第2行開始
Rem>>\“目錄”工作表A列,存放序號;B列存放“含義”;D列存放超鏈接
Rem>>\最多可以獲取9999個工作表目錄
Rem>>
For Each sht In ThisWorkbook.Worksheets
shtName = sht.Name
If shtName = '目錄' Then
MsgBox '本工作簿已存在《目錄》工作表,請確認' & Chr(13) & Chr(10) _
& '請手動刪除該工作表或者對其更名,將新建一個新的《目錄》工作表'
End
End If
Next
'新建目錄
ThisWorkbook.Sheets(1).Select
Sheets.Add
ActiveSheet.Name = '目錄'
Set shtContent = ThisWorkbook.Worksheets('目錄')
shtContent.Move before:=ThisWorkbook.Sheets(1)
shtContent.Cells.ClearContents
shtContent.Range('A1') = '序號'
shtContent.Range('B1') = '含義'
shtContent.Range('C1') = '工作表名稱'
shtContent.Range('D1') = '超鏈接'
strNewWbName = ThisWorkbook.Name
For Each sht In ThisWorkbook.Worksheets
shtName = sht.Name
If shtName <> '目錄' Then
For intI = 2 To 10000 Step 1
If shtContent.Cells(intI, 'C') = '' Then
shtContent.Cells(intI, 'A') = intI - 1
shtContent.Cells(intI, 'C') = shtName
strHyperLink = 'HYPERLINK(' & Chr(34) & '[' & strNewWbName & ']' & _
shtName & '!A1' & Chr(34) & ',' & Chr(34) & shtName & Chr(34) & ')'
shtContent.Cells(intI, 'D') = '=' & strHyperLink
Exit For
End If
Next
End If
Next
'調整單元格
shtContent.Rows.AutoFit
shtContent.Columns.AutoFit
shtContent.Columns('B:B').ColumnWidth = 20
With shtContent.Cells
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub 代碼截圖 Part 4:部分代碼解讀
以上為本次的學習內容,下回見
|
|
來自: 今天George > 《Excel VBA》