ExcelmacrosVBA基于活动单元格内容创build到另一个工作表的超链接
我正在尝试创build一个超链接,链接到基于单元格文本的同一工作簿中另一个工作表中的单元格。 我试图做手动,这是结果:
=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
– 单元格目的地的范围
友好的文本将是同一个当前单元格的内容。
我在这里特别对string操作有困难,所以我决定将代码分成几个variables,到目前为止,我无法产生所需的输出和结果到运行时错误'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
您的macros将工作,如果您replace:
ActiveCell.FormulaR1C1 = x & c & y & c & z
有:
ActiveCell.Formula = x & c & y & c & z
这假定Match()find它正在寻找的东西。
对于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
(假设您需要每个单元格的内容为“友好名称”)