使用公式生成超链接时,VBA 无法打开 Excel 超链接 [英] VBA to open Excel hyperlink does not work when hyperlink generated with a formula
问题描述
使用公式生成的 Excel 超链接似乎存在错误.我使用的是 Excel 2010.我有一个包含 URL 的单元格的电子表格,我的目标是做以下两件事:
- 将这些单元格变成超链接.
- 创建一个键盘快捷键来打开这些超链接,这样我就不必使用鼠标了.
要做#1,最初我只使用函数=HYPERLINK()
.因此,我的 URL 位于 A
列中,我使用此公式在 B
列中创建超链接.
要做#2,我创建了以下宏,它应该使用键盘快捷键Ctrl+H打开超链接:
Sub Open_Hyperlink()'' Open_Hyperlink 宏'' 键盘快捷键:Ctrl+h'Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True结束子
问题是这个宏似乎只适用于不是使用公式创建的超链接.例如,如果我只是在单元格 http://www.google.com
中键入,Excel 会自动将其设为超链接,并且键盘快捷键宏有效,但它不会使用公式生成的超链接.
我还注意到,当我右键单击公式生成的超链接时,下拉菜单中没有打开超链接的选项,但右键单击不是由公式生成的超链接时有该选项.
我找到了以下解决方法.我没有使用公式生成超链接,而是使用了一个我发现的宏
希望这在某种程度上有所帮助.祝你好运!
There seems to be a bug with Excel hyperlinks which are generated with a formula. I'm using Excel 2010. I have a spreadsheet with cells containing URLs, and my goal is to do the following two things:
- Turn these cells into hyperlinks.
- Create a keyboard shortcut to open these hyperlinks so I don't have to use the mouse.
To do #1, initially I just used the function =HYPERLINK()
. So, my URLs are in column A
, and I used this formula to make hyperlinks in column B
.
To do #2, I created the following macro which should open the hyperlink with the keyboard shortcut Ctrl+H:
Sub Open_Hyperlink()
'
' Open_Hyperlink Macro
'
' Keyboard Shortcut: Ctrl+h
'
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
The problem is that this macro only seems to work on hyperlinks which are not created using a formula. For example, if I just type into a cell http://www.google.com
, Excel will automatically make this a hyperlink and the keyboard shortcut macro works, where it doesn't with formula generated hyperlinks.
I've also noticed that when I right click on formula generated hyperlinks, there is no option in the drop-down menu to open the hyperlink, yet there is that option when right clicking on hyperlinks not generated by a formula.
I've found the following workaround. Rather than generate hyperlinks using a formula, I used a macro which I found here.
Sub HyperAdd()
'Converts each text hyperlink selected into a working hyperlink
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell
End Sub
I'm able to use the keyboard shortcut to open the hyperlinks generated with this macro. I'm wondering if anyone has or had a similar problem, and why the formula generated hyperlinks are not working for me. I would prefer to use formulas to make hyperlinks in the future, since it is simpler, so if anyone knows of a way to avoid using a macro to make hyperlinks, I'd really appreciate it.
I'm wondering if anyone has had a similar problem, and why the formula generated hyperlinks are not working for me.
Alas, this seems to be painful truth: Excel does not add to Hyperlinks
collection formula-generated links - below is the screen from the debugger which is pointed to =HYPERLINK("http://www.google.com/";"Google")
:
I'm not sure whether this is a deliberate implementation or a bug, but yes, formula-generated links may NOT be opened using Hyperlinks().Follow
method.
However, if you're going to use keyboard shortcut for links opening, just use the following code - it will automatically convert to clickable link selected cell text and open it:
Sub Open_Hyperlink()
Selection.Hyperlinks.Add Anchor:=Selection, Address:=Selection.Formula
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
Just assign any shortcut and you're ready to go) Here is the sample: https://www.dropbox.com/s/d4cie7lun22quma/FollowLinks.xlsm
Hope that's somehow helpful. Good luck!
这篇关于使用公式生成超链接时,VBA 无法打开 Excel 超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!