【发布时间】:2012-08-11 18:18:53
【问题描述】:
使用 Excel 2010,在每一行中,我想在 G 列中创建一个超链接。
此栏已包含信息,应用作超链接的显示文本(或友好名称)。
要使超链接指向正确的位置,它需要 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 不会有问题。
【问题讨论】:
-
哦,好的,这么快就回答了!已经非常感谢你了,我马上试试。
标签: vba excel hyperlink excel-2010