2016-05-14 106 views
1

我想創建一個超鏈接鏈接到基於單元格文本的同一工作簿中的另一工作表中的單元格。我試圖做手工,這裏是結果:Excel宏VBA創建一個基於活動單元格內容的另一個工作表的超鏈接

=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 

回答

1

您的宏將工作:

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

有:

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

這假定匹配()發現它正在尋找。

對於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 

(假設你需要在每個單元格的內容是「友好名稱」)

+0

這並獲得成功!非常感謝! – rmcalingasan

相關問題