首页 > 解决方案 > 为 FIND 函数找到的单元格创建超链接

问题描述

我编写了 VBA 代码来搜索“Sheet1”中的单词。单词列表在“Sheet2”中。结果与单词和在“Sheet1”中找到的单词的所有单元格地址一起发布在“Sheet3”中。

例如,单元格地址发布为“$B$26”。我希望这是到 Sheet1 的单元格 B26 的超链接。

我使用了下面的代码。

Worksheets("Sheet3").Activate

'Record the address of the data, in the current workbook.

     With ThisWorkbook.ActiveSheet.Range("D2")
        .Value = "Address of variable:"
        .Offset(0, -1).Value = "Variable Name"
        .Offset(0, -2).Value = "No of usages"
        .Offset(i, 0).Value = GCell.Address
        .Offset(i, -1).Value = Txt
        .Columns.AutoFit
        .Offset(i, 1).Columns.AutoFit

If GCell Is Nothing Then Exit Sub

    Sheets("Sheet3").Hyperlinks.Add Anchor:=Sheets("Sheet3").Cells(i,0), _
         Address:="", _
         SubAddress:="'" & Sheets("Sheet1").Name & "'!" & GCell.Address, TextToDisplay:="Click"

我明白了

运行时错误“1004”:应用程序定义的或对象定义的错误

在上面的线上。GCell是找到单词的范围。

标签: excelvba

解决方案


这里的问题是.Cells(i,0)

行/列编号以1not开头,0因此该列0不存在,因此您会收到错误消息。还要确保i>0.


我强烈建议您避免使用工作表.ActivateActiveSheet而是按名称引用您的工作表。您可能会从阅读 如何避免在 Excel VBA 中使用 Select 中受益。

这……</p>

Worksheets("Sheet3").Activate
With ThisWorkbook.ActiveSheet.Range("D2")

可以写成……</p>

With ThisWorkbook.Worksheets("Sheet3").Range("D2")

推荐阅读