使用宏生成宏按钮

时间:2016-02-01 17:31:00

标签: excel

使用Excel 2013,我希望将代码插入到预先存在的宏中,名为" Rebuild_TOC"在我的" TOC"上生成一个宏按钮调用" Repair_Back_To_TOC"的工作表,一个宏。我希望它出现在我的TOC上的单元格C2:E3,如果可能的话,命名为#34;重建回TOC超链接"。

如何使用我当前的代码完成此操作?:

Sub Rebuild_TOC()

Dim wbBook As Workbook
Dim wsActive As Worksheet
Dim wsSheet As Worksheet

Dim lnRow As Long
Dim lnPages As Long
Dim lnCount As Long

Set wbBook = ActiveWorkbook

With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
End With

' If the TOC sheet already exist delete it and add a new worksheet.
On Error Resume Next
Application.EnableEvents = False
With wbBook
    .Worksheets("TOC").Delete
    .Worksheets.Add Before:=.Worksheets(1)
End With
Application.EnableEvents = True
On Error GoTo 0

Set wsActive = wbBook.ActiveSheet
With wsActive
    .Name = "TOC"
    With.Range("A1:B1")
        .Value = Array("Table of Contents", "Sheet # – # of Pages")
        .Font.Bold = True
    End With
End With

lnRow = 2
lnCount = 1

' Iterate through the worksheets in the workbook and create sheetnames, add hyperlink
' and count & write the running number of pages to be printed for each sheet on the TOC.
For Each wsSheet In wbBook.Worksheets
    If wsSheet.Name <> wsActive.Name Then
        wsSheet.Activate
        With wsActive
            .Hyperlinks.Add .Cells(lnRow, 1), "", _
            SubAddress:= wsSheet.Name & "!A1", _
            TextToDisplay:=wsSheet.Name
            lnPages = wsSheet.PageSetup.Pages().Count
            .Cells(lnRow, 2).Value = "‘" & lnCount & "-" & lnPages
        End With
        lnRow = lnRow + 1
        lnCount = lnCount + 1
    End If
Next wsSheet

wsActive.Activate
wsActive.Columns("A:B").EntireColumn.AutoFit

With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
End With
End Sub

0 个答案:

没有答案
相关问题