【问题标题】:How can I create one hyperlink to each worksheet in one index sheet?如何在一个索引表中为每个工作表创建一个超链接?
【发布时间】:2013-12-12 06:15:37
【问题描述】:

编辑: 在做了更多研究后,我偶然发现了这个handy little shortcut: 只需右键单击左下角的小箭头即可显示所有工作表 - 无需代码!


我有一个包含 100 个选项卡的 Excel 工作簿。幸运的是,标签的编号都是 1-100。我有一个索引页,其中包含一行中的所有数字,我想在该行旁边创建一行,并带有指向编号选项卡的超链接。

   A        B
---------------------------
|  1   | link to tab 1    |
---------------------------
|  2   | link to tab 2    |
---------------------------

等等……

到目前为止,我发现的最有希望的是:

=Hyperlink(“C:\Documents and Settings\Admin1\Desktop\” & A1 & “.xls”,A1)

我知道超链接函数期望:

=HYPERLINK(link_location,friendly_name)

当我手动操作时,我得到了这个:

=HYPERLINK('1'!$A$1,A1)

所以我想做这样的事情:

=HYPERLINK('& A1 &'!$A$1,A1)   

但它不起作用。任何帮助深表感谢。此外,如果有更简单的方法来解决这个问题 - 我全神贯注。

【问题讨论】:

    标签: excel hyperlink vba


    【解决方案1】:

    使用类似这样的代码

    1. 按 Alt + F11 打开 Visual Basic 编辑器 (VBE)。
    2. 从菜单中选择插入模块。
    3. 将代码粘贴到右侧代码窗口中。
    4. 关闭 VBE,如果需要,保存文件。

    中转到Tools-Macro-Macros 并双击CreateTOC
    中单击开发人员选项卡代码组中的Macros button,然后单击列表框中的CreateTOC

    Option Explicit
    
    Sub CreateTOC()
        Dim ws As Worksheet
        Dim nmToc As Name
        Dim rng1 As Range
        Dim lngProceed As Boolean
        Dim bNonWkSht As Boolean
        Dim lngSht As Long
        Dim lngShtNum As Long
        Dim strWScode As String
        Dim vbCodeMod
    
        'Test for an ActiveWorkbook to summarise
        If ActiveWorkbook Is Nothing Then
            MsgBox "You must have a workbook open first!", vbInformation, "No Open Book"
            Exit Sub
        End If
    
        'Turn off updates, alerts and events
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
        End With
    
        'If the Table of Contents exists (using a marker range name "TOC_Index") prompt the user whether to proceed
        On Error Resume Next
        Set nmToc = ActiveWorkbook.Names("TOC_Index")
        If Not nmToc Is Nothing Then
            lngProceed = MsgBox("Index exists!" & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbCritical, "Warning")
            If lngProceed = vbYes Then
                Exit Sub
            Else
                ActiveWorkbook.Sheets(Range("TOC_Index").Parent.Name).Delete
            End If
        End If
        Set ws = ActiveWorkbook.Sheets.Add
        ws.Move before:=Sheets(1)
        'Add the marker range name
        ActiveWorkbook.Names.Add "TOC_INDEX", ws.[a1]
        ws.Name = "TOC_Index"
        On Error GoTo 0
    
        On Error GoTo ErrHandler
    
        For lngSht = 2 To ActiveWorkbook.Sheets.Count
            'set to start at A6 of TOC sheet
            'Test sheets to determine whether they are normal worksheets
            ws.Cells(lngSht + 4, 2).Value = TypeName(ActiveWorkbook.Sheets(lngSht))
            If TypeName(ActiveWorkbook.Sheets(lngSht)) = "Worksheet" Then
                'Add hyperlinks to normal worksheets
                ws.Hyperlinks.Add Anchor:=ws.Cells(lngSht + 4, 1), Address:="", SubAddress:="'" & ActiveWorkbook.Sheets(lngSht).Name & "'!A1", TextToDisplay:=ActiveWorkbook.Sheets(lngSht).Name
            Else
                'Add name of any non-worksheets
                ws.Cells(lngSht + 4, 1).Value = ActiveWorkbook.Sheets(lngSht).Name
                'Colour these sheets yellow
                ws.Cells(lngSht + 4, 1).Interior.Color = vbYellow
                ws.Cells(lngSht + 4, 2).Font.Italic = True
                bNonWkSht = True
            End If
        Next lngSht
    
        'Add headers and formatting
        With ws
            With .[a1:a4]
                .Value = Application.Transpose(Array(ActiveWorkbook.Name, "", Format(Now(), "dd-mmm-yy hh:mm"), ActiveWorkbook.Sheets.Count - 1 & " sheets"))
                .Font.Size = 14
                .Cells(1).Font.Bold = True
            End With
            With .[a6].Resize(lngSht - 1, 1)
                .Font.Bold = True
                .Font.ColorIndex = 41
                .Resize(1, 2).EntireColumn.HorizontalAlignment = xlLeft
                .Columns("A:B").EntireColumn.AutoFit
            End With
        End With
    
        'Add warnings and macro code if there are non WorkSheet types present
        If bNonWkSht Then
            With ws.[A5]
                .Value = "This workbook contains at least one Chart or Dialog Sheet. These sheets will only be activated if macros are enabled (NB: Please doubleclick yellow sheet names to select them)"
                .Font.ColorIndex = 3
                .Font.Italic = True
            End With
            strWScode = "Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)" & vbCrLf _
                        & "     Dim rng1 As Range" & vbCrLf _
                        & "     Set rng1 = Intersect(Target, Range([a6], Cells(Rows.Count, 1).End(xlUp)))" & vbCrLf _
                        & "     If rng1 Is Nothing Then Exit Sub" & vbCrLf _
                        & "     On Error Resume Next" & vbCrLf _
                        & "     If Target.Cells(1).Offset(0, 1) <> ""Worksheet"" Then Sheets(Target.Value).Activate" & vbCrLf _
                        & "     If Err.Number <> 0 Then MsgBox ""Could not select sheet"" & Target.Value" & vbCrLf _
                        & "End Sub" & vbCrLf
    
            Set vbCodeMod = ActiveWorkbook.VBProject.VBComponents(ws.CodeName)
            vbCodeMod.CodeModule.AddFromString strWScode
        End If
    
        'tidy up Application settins
        With Application
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With
    
    ErrHandler:
        If Err.Number <> 0 Then MsgBox Err.Description & vbCrLf & "Please note that your Application settings have been reset", vbCritical, "Code Error!"
    End Sub
    

    【讨论】:

    • + 1 简直太美了:)
    • 哇——太棒了!谢谢:)
    • 能否在所有引用 TOCIndex 的工作表中添加超链接?
    【解决方案2】:

    我的sn-p:

            Sub AddLinks()
                Dim wksLinks As Worksheet
                Dim wks As Worksheet
                Dim row As Integer
                Set wksLinks = Worksheets("Links")
                wksLinks.UsedRange.Delete
                row = 1
                For Each wks In Worksheets
                   ' Debug.Print wks.Name
                    wks.Hyperlinks.Add wksLinks.Cells(row, 1), "", wks.Name & "!A1", , wks.Name
                    row = row + 1
                Next wks
            End Sub
    

    假设有一个名为“链接”的工作表

    【讨论】:

      【解决方案3】:

      可能不是你的方法的直接答案,但我会创建一些更令人赏心悦目的东西,比如......一些形状格式很好,然后为它们分配一些基本宏,用于选择床单。 这可以轻松修改以转到特定地址(例如转到 Ctrl+G内置 Excel 功能)。希望这有助于您的文件的时尚风格 :)

      编辑!

      不知道为什么我的回答获得了 -1 分。正如我所说,这是对给定问题的替代方案,而不是直接解决方案。尽管如此,我确实相信我最初的答案是肤浅的,没有经过验证/工作的 VBA 代码,因此我在下面开发了一个实用的例子:

      Sub Add_Link_Buttons()
              'Clear any Shapes present in the "Links" sheet
          For j = ActiveSheet.Shapes().Count To 1 Step -1
          ActiveSheet.Shapes(j).Delete
          Next j
              'Add the shapes and then asign the "Link" Macros
          For i = 1 To ActiveWorkbook.Sheets.Count
          ActiveSheet.Shapes.AddShape Type:=msoShapeRoundedRectangle, Left:=50, Top:=i * 25, Width:=100, Height:=25
          ActiveSheet.Shapes(i).OnAction = "Select_Sheet" & i
          'even add the the sheet Name as Test:
          ActiveSheet.Shapes(i).TextFrame2.TextRange.Characters.Text = Sheets(i).Name
          Next i
      End Sub
      

      “基本选择宏”应该在哪里:

      Sub Select_Sheet1()
          ActiveWorkbook.Sheets(1).Select
      End Sub
      Sub Select_Sheet2()
          ActiveWorkbook.Sheets(2).Select
      End Sub
      Sub Select_Sheet3()
          ActiveWorkbook.Sheets(3).Select
      End Sub
      ' and so on!
      ' Note! to link a specific address within the sheets use the range like in 'Sheets(1).Range("A1").Select
      

      同样,这是一种替代方法,不添加超链接(如要求的那样),但允许从同一位置选择工作表。

      要将按钮指向外部文件的链接,只需定义 address > filename/workbook Sheets()Open ;)

      【讨论】:

        【解决方案4】:

        这是我使用的代码:

        Sub CreateIndex()
        
            'This macro checks for an Index tab in the active worksheet and creates one if one does not already exist.
            'If an Index tab already exists, the user is asked to continue.  If they continue, the original Index tab is replaced by a new Index tab.  If they do not continue, the macro stops.
            'The user is then asked if they want to create a link back to the Index tab on all other worksheets (yes or no) and the macro acts accordingly.
        
            Dim wsIndex As Worksheet
            Dim wSheet  As Worksheet
            Dim retV    As Integer
            Dim i       As Integer
        
            With Application
                .DisplayAlerts = False
                .ScreenUpdating = False
            End With
        
            Set wsIndex = Worksheets.Add(Before:=Sheets(1))
        
            With wsIndex
        
                On Error Resume Next
                    .Name = "Index"
                    If Err.Number = 1004 Then
                        If MsgBox(Prompt:="A sheet named ""Index"" already exists. Do you wish to continue by replacing it with a new Index?", _
                        Buttons:=vbInformation + vbYesNo) = vbNo Then
                            .Delete
                            MsgBox "No changes were made."
                            GoTo EarlyExit:
                    End If
                        Sheets("Index").Delete
                        .Name = "Index"
                    End If
        
                On Error GoTo 0
        
            retV = MsgBox("Create links back to ""Index"" sheet on other sheets?", vbYesNo, "Linking Options")
        
                    For Each wSheet In ActiveWorkbook.Worksheets
                    If wSheet.Name <> "Index" Then
                        i = i + 1
                        If wSheet.Visible = xlSheetVisible Then
                            .Range("B" & i).Value = "Visible"
                        ElseIf wSheet.Visible = xlSheetHidden Then
                            .Range("B" & i).Value = "Hidden"
                        Else
                            .Range("B" & i).Value = "Very Hidden"
                        End If
        
                    .Hyperlinks.Add Anchor:=.Range("A" & i), Address:="", SubAddress:="'" & wSheet.Name & "'!A1", TextToDisplay:=wSheet.Name
                    If retV = 6 And wSheet.Range("A1").Value <> "Index" Then
                        wSheet.Rows(1).Insert
                        wSheet.Range("A1").Hyperlinks.Add Anchor:=wSheet.Range("A1"), Address:="", SubAddress:="'" & .Name & "'!A1", TextToDisplay:=.Name
                    End If
        
                    End If
                Next wSheet
        
                .Rows(1).Insert
                With .Rows(1).Font
                    .Bold = True
                    .Underline = xlUnderlineStyleSingle
                End With
        
                .Range("A1") = "Sheet Name"
                .Range("B1") = "Status"
                .UsedRange.AutoFilter
                Rows("2:2").Select
                ActiveWindow.FreezePanes = True
                Application.Goto Reference:="R1C1"
        
                .Columns("A:B").AutoFit
            End With
        
            With ActiveWorkbook.Sheets("Index").Tab
                .Color = 255
                .TintAndShade = 0
            End With
        
            EarlyExit:
            With Application
                .DisplayAlerts = True
                .ScreenUpdating = True
            End With
        End Sub
        

        -迈克

        【讨论】:

          猜你喜欢
          • 2021-05-31
          • 2020-03-04
          • 2020-09-24
          • 1970-01-01
          • 1970-01-01
          • 2022-01-28
          • 1970-01-01
          • 1970-01-01
          • 2017-06-14
          相关资源
          最近更新 更多