2016-05-14 103 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

相关问题