VBA为一列中的每个单元格添加超链接功能

问题描述 投票:0回答:1

你好,先谢谢你的帮助。目前我创建了下面的Macro,用超链接功能更新范围。这很好用。然而,问题是这个数据通过一个数据源被刷新。一旦刷新,列A中就会出现空白,当刷新的数据源发生时,是否有办法执行?

Sub InsertHyperlinkFormulaInCell()

ActiveWorkbook.Worksheets("Query").Range("A2:A20000").Formula = 
"=HYPERLINK(CONCAT(x2,B2),W2)"

End Sub

任何帮助都将是非常感激的。谢谢您的帮助

enter image description here

excel excel-vba excel-formula
1个回答
1
投票

你需要在行中循环以改变每一行的超链接,否则你只是改变了第1行.如果你想在工作表中保留Excel引用,你可以在for循环(或在本例中的while循环)中交换行号,这有时是很好的,但它使得在连接过程中的代码很笨拙。

Sub InsertHyperlinkFormulaInCell()

    'starting with row 2 since row 1 is title row
    currentRow = 2

    ' cells allows to call columns by number instead of letters (so A becomes 1, B:2 etc.)
    While Cells(currentRow, 2) <> "" 'check whether Column B is empty, stop if it is
        'This is your active line, just replaced the row number 1 with the variable currentRow
        'which loops through the sheet until it encounters and empty cell in column B
        ActiveWorkbook.Worksheets("Sheet1").Cells(currentRow, 1) = "=HYPERLINK(CONCAT(B" & currentRow & ",C" & currentRow & "),D" & currentRow & ")"
        currentRow = currentRow + 1
    Wend

End Sub

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.