使用工作表编号创建超链接

时间:2018-04-22 14:51:04

标签: vba excel-vba excel

我想创建通过vba链接到不同工作表的超链接。在我的工作簿中,工作表名称为"股票代码1","股票代码2"等等。我想在工作表上创建超链接,即"索引页面"在相同的工作簿中但条件是超链接将在列#34; M"中的最后一次使用行之后开始。 (已使用的列已经超链接到其工作表),工作表名称将显示为数字,如" Stock Code 1000"它将显示为1000

示例:

超链接要求:"股票代码1001到股票代码1010" 上次使用的列:M1000 超链接从:M1001到M1010 超链接数据:1001到1010

我有以下代码将新工作表插入我的工作簿。是否有任何技巧来创建其中的新工作表的超链接?

Sub InsertSheets()
Dim numSheets As Integer
Dim i As Long
Dim lastName As String
Dim lastNum As Long

' Find name of last sheet
lastName = Sheets(Sheets.Count).Name

' Get last number from name
lastNum = Mid(lastName, InStrRev(lastName, " ") + 1) + 0

' Ask for how many sheets to create
numSheets = Application.InputBox("How many sheets do you want to insert?")

' If they entered a number greater than 0, Add sheets

If numSheets >= 1 Then
For i = (lastNum + 1) To (lastNum + numSheets)
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Stock Code " & i

Next i
End If
End Sub

需要帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

在VBA中,您将使用超链接对象集合。 (请参阅此处的参考资料:Hyperlinks.Add Method (Excel)

以下是一些 可帮助您使用Excel中的超链接 的链接:

  1. 首先手动,以便了解如何添加超链接:
     How to add a hyperlink to another sheet
  2. 然后如何使用 VBA 执行此操作:
    Add Hyperlinks in Excel using VBA
    Hyperlinks in Excel VBA – Explained with Examples!
  3. 您还需要知道:

    以及如何将其作为对象引用:

    • Referring to Cells by Using a Range Object

      Sub InsertSheets()
          Dim lastName As String
          Dim lastNum As Long
          Dim numSheets As Integer
          Dim i As Long
          Dim linkEntryRow As Long
      
          ' Find name of last sheet
          lastName = Sheets(Sheets.Count - 1).Name
      
          ' Get last number from name
          lastNum = Mid(lastName, InStrRev(lastName, " ") + 1) + 0
      
          ' Ask for how many sheets to create
          numSheets = Application.InputBox("How many sheets do you want to insert?")
      
          ' If they entered a number greater than 0, Add sheets
          If numSheets >= 1 Then
              For i = (lastNum + 1) To (lastNum + numSheets)
                  'Find the last non-blank cell in column M (column 13) and go to the next row for a blank cell.
                  linkEntryRow = Worksheets("Index Page").Cells(Rows.Count, 13).End(xlUp).Offset(1, 0).Row
                  Sheets.Add After:=Sheets(Sheets.Count - 1)
                  ActiveSheet.Name = "Stock Code " & i
                  ActiveSheet.Hyperlinks.Add Anchor:=Worksheets("Index Page").Range("M" & CStr(linkEntryRow)), _
                      Address:="", SubAddress:="'" & ActiveSheet.Name &         "'!A1", TextToDisplay:=CStr(i)
              Next i
          End If
      End Sub