簡體   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