使用来自两列的信息创build超链接

使用Excel 2010,在每一行中,我想在列G中创build一个超链接。

该列已经保存了应当用作超链接的显示文本(或友好名称)的信息。

要使超链接指向正确的位置,它需要列M保存的编号。 匹配信息总是在同一行中。

所以基本上,这可能只是特定行的两个单元格中两位信息的合并。

这是我开始的:

Sub Macro2() ' ' Macro2 Macro ' Dim Name As String Dim Branch As String ' Combination of two rows Dim CombineRow As Range Dim cell As Range Dim row As Range Dim Branch_ID As Range Set Branch_ID = Worksheets("Default").Range("M2") Set CombineRow = Range("G2:M2") For Each row In CombineRow.Rows For Each cell In row.Cells Branch = Branch_ID.Cells.Value Name = Name_ID.Cells.Value ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _ "https://www.example.com/", SubAddress:= _ "something:", _ TextToDisplay:=Name Next cell ' relative one row down Set Branch_ID = Branch_ID.Offset(1, 0) Set Name_ID = Branch_ID.Offset(1, 0) Next row End Sub 

我不知道如何将列M的值(例如600021610 )附加到超链接的末尾?

我在这里正确的道路上?

编辑:

我现在拥有的是这样的:

 Sub Macro2() ' ' Macro2 Macro ' Dim Branch As String Dim Name As String ' combine two rows Dim CombineRow As Range Dim cell As Range Dim row As Range Dim Branch_ID As Range Dim Name_ID As Range Set Branch_ID = Worksheets("Default").Range("M2") Set Name_ID = Worksheets("Default").Range("G2") Set CombineRow = Range("G2:M2") For Each row In CombineRow.Rows For Each cell In row.Cells Branch = Branch_ID.Cells.Value Name = Name_ID.Cells.Value ActiveSheet.Hyperlinks.Add Anchor:=Name_ID, Address:= _ "https://www.example.com/", SubAddress:= _ "something" & Branch, _ TextToDisplay:=Name Next cell Set Branch_ID = Branch_ID.Offset(1, 0) Set Name_ID = Name_ID.Offset(1, 0) Next row End Sub 

这工作,但只为第一行。 为什么不做循环? CombineRow真的是这样的,还是一定是这样的: G:G;M:M或者类似的东西?

我也可以想象使用一个Do ... While循环,因为放入一个end_tag不会是一个问题。

尝试使参数超链接.add使用一个variables。

 dim addressStr as string dim targetRng as Range For Each row In CombineRow.Rows For Each cell In row.Cells Branch = Branch_ID.Cells.Value Name = Name_ID.Cells.Value set targetRng = range("A1") 'change this to whatever you want it to be each loop addressStr = "https://www.example.com/" & cells(cell.row,15).value ActiveSheet.Hyperlinks.Add Anchor:=targetRng , Address:= _ addressStr, SubAddress:= _ "something:", _ TextToDisplay:=Name Next cell ' relative one row down Set Branch_ID = Branch_ID.Offset(1, 0) Set Name_ID = Branch_ID.Offset(1, 0) Next row<BR> End Sub<BR> 

此外,您正在设置Name_ID和Branch_ID在您的偏移相同的值。


在你的循环中,你将只循环1行(因为你的范围都在第2行)。 所以你唯一的循环是单元格 – 你不会设置分支/名称ID的不同,因为它们发生在循环之后:

 For Each cell In row.Cells 'stuff Next cell Set Branch_ID = Branch_ID.Offset(1, 0) Set Name_ID = Name_ID.Offset(1, 0) 

所以每次调用超链接函数时都使用相同的Name_ID范围。 您也可能想要使这些偏移(0,1),因为偏移是偏移(行,列),似乎你正在迭代水平范围。

您可以简单地使用&运算符连接string并创buildURL。

 addressStr = "https://www.example.com/" & <<Specify Cell for M Column and Row>> 

有用!
感谢大家的帮助。
这个soltuion不是要把(G2:M2),因为这导致只创build七个超链接。
相反,只要将(G:M)和IF放在它的周围,以防止在单元格已经空时创build无意义的超链接。

  Sub Macro2() ' ' Macro2 Macro ' Dim Branch As String Dim Name As String ' combine two rows Dim CombineRow As Range Dim cell As Range Dim row As Range Dim Branch_ID As Range Dim Name_ID As Range Set Branch_ID = Worksheets("Default").Range("M2") Set Name_ID = Worksheets("Default").Range("G2") Set CombineRow = Range("G:M") For Each row In CombineRow.Rows For Each cell In row.Cells Branch = Branch_ID.Cells.Value Name = Name_ID.Cells.Value If Name <> "" Then ActiveSheet.Hyperlinks.Add Anchor:=Name_ID, Address:= _ "https://www.example.com", SubAddress:= _ "something" & Branch, _ TextToDisplay:=Name Set Branch_ID = Branch_ID.Offset(1, 0) Set Name_ID = Name_ID.Offset(1, 0) End If Next cell Next row End Sub 

我确信有很大的改善空间,特别是在中频build设方面。
但到目前为止,它的工作。 这并不意味着任何人都应该阻止他们改进这一点的意见;)。 这些基本上是我在VBA的第一步! 🙂