如果以后我从其中任何一个删除了任何超链接,为什么Excel删除所有粘贴的超链接?

发布于 2025-01-27 16:15:56 字数 307 浏览 0 评论 0原文

我正在使用以下代码将超链接从一个单元格复制到一个范围。
超链接已被粘贴并尽其所能,
但是,如果我以后从任何一个粘贴的超链接中删除了任何超链接,则删除了目的地单元格的所有超链接!
这个问题发生在我使用VBA(复制,粘贴& delete)或仅使用Excel。
我正在使用Office 2016。

Sub Hyperlinks_Issue()
 
    Range("R2").Copy Range("N2:N15")
 
    Range("N2").Hyperlinks.Delete
 
End Sub

I am using the below code to copy hyperlink from one cell to a range.
The hyperlinks have been pasted and working as it should,
But if I later removed any hyperlink from any one of pasted hyperlinks ,then all hyperlinks from destination cells are deleted !!
This issue happens either I have used vba (copy, paste & delete) or used merely excel.
I am using office 2016.

Sub Hyperlinks_Issue()
 
    Range("R2").Copy Range("N2:N15")
 
    Range("N2").Hyperlinks.Delete
 
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2025-02-03 16:15:56

问题与以下事实有关。代码> N2:N15 。您可以通过立即插入以下代码来检查此问题:

debug.print(range(“ n2:n15”)。hyperlinks.count)

此返回1,意味着整个范围只有一个超链接(如您所期望的,而不是14个)。如果然后检查此信息:

debug.print(range(“ n2”)。超链接(1).range.Address)

您会看到它返回$ n $ 2:$ n $ N $ 15 < /代码>。因此,当您使用range(“ N2”)时。超链接,您只是删除附加到整个范围的一个一个超链接。

为避免这种情况,请循环遍历目的地范围内的所有单元格,并分别为每个单元格附加超链接。例如

Sub copyHyperlinks()

For Each myCell In Range("N2:N15")

    Range("R2").Copy myCell

Next myCell

Debug.Print (Range("N2:N15").Hyperlinks.Count) '= 14

End Sub

,例如,range(“ n2”)。超链接仅在此特定单元格中删除超链接。

The problem has to do with the fact that Range("R2").Copy Range("N2:N15") attaches the hyperlink in R2 to the entire range of N2:N15. You can check this by inserting the following code immediately afterwards:

Debug.Print (Range("N2:N15").Hyperlinks.Count)

This returns 1, meaning the entire range only has one hyperlink (instead of 14, as you might be expecting). If then you check this:

Debug.Print (Range("N2").Hyperlinks(1).Range.Address)

You will see that it returns $N$2:$N$15. Hence, when you use Range("N2").Hyperlinks.Delete, you are simply deleting the one hyperlink that is attached to the entire range.

To avoid this, loop through all the cells in your destination range and attach the hyperlink for each cell individually. E.g.

Sub copyHyperlinks()

For Each myCell In Range("N2:N15")

    Range("R2").Copy myCell

Next myCell

Debug.Print (Range("N2:N15").Hyperlinks.Count) '= 14

End Sub

Now, Range("N2").Hyperlinks.Delete will only delete the hyperlink in this particular cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文