将数组的内容作为超链接输出到单元格

时间:2014-08-10 03:21:40

标签: vba

假设数组从0到2(但并非总是如此)。在不同情况下,预计尺寸会发生变化。此外,信息分为3个元素,第一个元素是单元格地址,第二个元素是找到单元格的工作表名称,第三个元素是工作簿名称。我希望输出的其余部分跟随同一列,这将是A,所以我想第0列。顺便说一下,我想把它作为一个函数,而不是作为一个子程序,因为我正在传递数组。

此外,我尝试编写一个函数来将超链接输出到单元格A1的新工作表

Private Function DisplayResults(cellAddressArr As Variant)

Dim WS As Worksheet
Dim sht As Worksheet
Dim s As String
Dim Value As String
Dim c As Range
Dim i As Long
Dim j As Long

Set WS = Sheets.Add
WS.Range("A1") = "Cell location"

j = 0
i = 0

Do
    'the WS.Hyperlinks.Add Anchor line is incorrectly formatted, or something is
    'entirely wrong with my logic behind outputting my array...  
    WS.Hyperlinks.Add Anchor:=WS.Range("A & j + 2"), Address:="labInventory" & "\" &
    cellAddressArr(i + 2), SubAddress:=cellAddressArr(i + 1) & "!" & cellAddressArr(i), 
    TextToDisplay:="Link"
    i = 1 + 3
    j = j + 1
Loop While i <> UBound(cellAddressArr)
End Function

http://gyazo.com/0cf8adc013f0d5c8137586162e431b3b,这是数组内容的一个例子

1 个答案:

答案 0 :(得分:0)

尝试更改:

WS.Hyperlinks.Add Anchor:=WS.Range("A & j + 2"), Address:="labInventory" & "\" &
    cellAddressArr(i + 2), SubAddress:=cellAddressArr(i + 1) & "!" & cellAddressArr(i), 
    TextToDisplay:="Link"

到:

WS.Hyperlinks.Add Anchor:=WS.Range("A" & j + 2), Address:="labInventory" & "\" &
    cellAddressArr(i + 2), SubAddress:=cellAddressArr(i + 1) & "!" & cellAddressArr(i), 
    TextToDisplay:="Link"

:)