excel - 为 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
是找到单词的范围。
解决方案
这里的问题是.Cells(i,0)
。
行/列编号以1
not开头,0
因此该列0
不存在,因此您会收到错误消息。还要确保i
是>0
.
我强烈建议您避免使用工作表.Activate
,ActiveSheet
而是按名称引用您的工作表。您可能会从阅读
如何避免在 Excel VBA 中使用 Select 中受益。
这……</p>
Worksheets("Sheet3").Activate
With ThisWorkbook.ActiveSheet.Range("D2")
可以写成……</p>
With ThisWorkbook.Worksheets("Sheet3").Range("D2")
推荐阅读
- linux - 防止应用程序执行警告向 VSO 输出错误
- cross-browser - 跨不同浏览器的 Web 推送通知字符数限制
- php - Symfony 3.3.18:作曲家更新/安装/需要火 => 内存不足
- r - 在 R-markdown 中保持表格位置
- python - Pytorch 量化运行时错误:尝试创建具有负维度的张量
- gcc - 为什么 GCC 会自己编译 3 次?
- python - 在相等的列值上切片熊猫数据框
- flutter - 如何在 ListView 中将项目居中
- python - 从单元测试 Python 解析 XML 文件
- matlab - 如何克服这个错误?试图访问 U(2,0);索引必须是正整数或逻辑