繁体   English   中英

如何使用VBA链接到Excel中的另一个工作表?

[英]How do I link to another sheet in Excel with VBA?

我正在尝试在excel中生成超链接列表,并将它们动态链接到其他工作表中的另一个单元格。 有人可以解释我如何格式化参考吗? 当前,它看起来像这样:

'p2r is one sheet
'sh is another
'Subaddress is currently linking to the correct location, wrong sheet.
    '(It's linking to [p2r]'s cells, not [sh]'s cells)

p2r.Hyperlinks.Add_
    Anchor:=p2r.Cells(p2rIndex, 1), _
    Address:="", _
    SubAddress:=sh.Cells(round2, 2).Address, _
    TextToDisplay:=PrevRow

谢谢您的帮助! 我看到其他人使用的格式为:Sheet!A1,但是当我尝试类似的格式时:

SubAddress:=sh & "!" & Cells(round2, 2).Address

我没有价值的结果。

您可以使用两种修改来使代码正常工作:

  1. 你可以用

    SubAddress:="'" & Replace(sh.Name, "'", "''") & "'!" & Cells(round2, 2).Address

    为了生成SubAddress:="'Sheet name'!$A$1"

    (公式与处理中的位' s为允许,如果有空间等,在工作表名称它仍然可以工作。如果你知道,不会因为你所定义的表名和没有其他人出现,例如,将对其进行更改,您可以将其简化为SubAddress:=sh.Name & "!" & Cells(round2, 2).Address


  1. 你可以用

    SubAddress:=sh.Cells(round2, 2).Address(External:=True)

    会生成SubAddress:="'[Book1.xlsx]Sheet name'!$A$1"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM