繁体   English   中英

Excel 宏 VBA 根据活动单元格内容创建指向另一个工作表的超链接

[英]Excel Macro VBA to create a hyperlink to another sheet based on the active cell content

我正在尝试创建一个超链接,该链接链接到基于单元格文本的同一工作簿中另一个工作表中的单元格。 我尝试手动完成,结果如下:

    =HYPERLINK("[Five Ecom - Floor Plan.xlsx]Inventory!" & 
     ADDRESS(MATCH('8th'!F9,Inventory!$A$1:$A$2000,0),1),'8th'!F9)
  • '8th'!F9 - 是当前单元格
  • Inventory!$A$1:$A$2000 - 单元格目的地的范围

友好文本将是同一当前单元格的内容。

我在这里的字符串操作特别困难,所以我决定将代码分成几个变量,到目前为止我无法产生所需的输出和结果为运行时错误“1004”。

Public Sub Convert_To_Hyperlinks()
    Dim cell As Range
    Dim x As String
    Dim y As String
    Dim z As String
    Dim c As String

    For Each cell In Intersect(Selection, ActiveSheet.UsedRange)
        If cell <> "" Then
            c = ActiveCell.Value
            x = "=HYPERLINK(""[Five Ecom - Floor Plan.xlsm]Inventory!"" & ADDRESS(MATCH("""
            y = """, Inventory!$A$1:$A$2000,0),1),"""
            z = """)"
            ActiveCell.FormulaR1C1 = x & c & y & c & z
        End If
    Next
End Sub

如果您替换,您的宏将起作用:

ActiveCell.FormulaR1C1 = x & c & y & c & z

和:

ActiveCell.Formula = x & c & y & c & z

这假设Match()找到了它正在寻找的东西。

对于Selection所有单元格,请使用:

Public Sub Convert_To_Hyperlinks()
    Dim cell As Range
    Dim x As String
    Dim y As String
    Dim z As String
    Dim c As String

    For Each cell In Intersect(Selection, ActiveSheet.UsedRange)
        If cell <> "" Then
            c = cell.Value
            x = "=HYPERLINK(""[Five Ecom - Floor Plan.xlsm]Inventory!"" & ADDRESS(MATCH("""
            y = """, Inventory!$A$1:$A$2000,0),1),"""
            z = """)"
            cell.Formula = x & c & y & c & z
        End If
    Next
End Sub

(假设您需要每个单元格的内容为“友好名称”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM