gpt4 book ai didi

excel - 为 FIND 函数找到的单元格创建超链接

转载 作者:行者123 更新时间:2023-12-03 03:14:09 24 4
gpt4 key购买 nike

我编写了 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"

我明白

Run time error '1004': Application-defined or object-defined error

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

最佳答案

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

行/列编号以 1 开头,而不是以 0 开头,因此列 0 不存在,因此会出现错误。还要确保 i>0

<小时/>

我强烈建议避免使用 .ActivateActiveSheet,而是通过其名称引用您的工作表。你可能会从阅读中受益 How to avoid using Select in Excel VBA .

这……

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

可以写成...

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

关于excel - 为 FIND 函数找到的单元格创建超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54686690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com