在Userform VBA中插入超链接

时间:2017-06-28 05:28:10

标签: excel vba excel-vba userform

我使用userform在excel中创建了一个欢迎注释,每次打开工作簿时都会显示6秒钟。我在该用户表单上插入了一个超链接。但是这个超链接不起作用。 我在那个标签上使用了以下代码..

Private Sub Label2_Click()

    Link = "https://www.healthindiatpa.com/"

    On Error GoTo NoCanDo

    ActiveWorkbook.FollowHyperlink Address:=Link, NewWindow:=True

    Unload Me

    Exit Sub

NoCanDo:

    MsgBox "Cannot open " & Link

End Sub

但它不起作用。每当我打开工作簿时点击该标签或超链接,网站都不会打开。超链接不起作用。 Userform image

1 个答案:

答案 0 :(得分:0)

此代码段中出现问题:

Private Sub UserForm_Activate()
Application.Wait (Now + TimeValue("00:00:01"))
UserForm1.Label1.Caption = "Loading Data..."
UserForm1.Repaint
Application.Wait (Now + TimeValue("00:00:03"))
UserForm1.Label1.Caption = "Please make sure Database file is open..."
UserForm1.Repaint
Application.Wait (Now + TimeValue("00:00:02"))
UserForm1.Label1.Caption = "Opening..."
UserForm1.Repaint
Application.Wait (Now + TimeValue("00:00:01"))
Unload UserForm1
End Sub

应用程序正在等待(因此不接受任何输入)。这就是为什么你的事件不会触发,因为在等待之后它立即继续下一行(这是标签更改并重新绘制)。

您可以通过更改此例程来解决此问题:

Private Sub UserForm_Activate()
Application.Wait (Now + TimeValue("00:00:01"))
DoEvents 'Allow for the label click to trigger!!!
UserForm1.Label1.Caption = "Loading Data..."
UserForm1.Repaint
Application.Wait (Now + TimeValue("00:00:03"))
DoEvents 'Allow for the label click to trigger!!!
UserForm1.Label1.Caption = "Please make sure Database file is open..."
UserForm1.Repaint
Application.Wait (Now + TimeValue("00:00:02"))
DoEvents 'Allow for the label click to trigger!!!
UserForm1.Label1.Caption = "Opening..."
UserForm1.Repaint
Application.Wait (Now + TimeValue("00:00:01"))
DoEvents 'Allow for the label click to trigger!!!
Unload UserForm1
End Sub

它不是很干净,但这样你就可以输入事件代码并修复你的问题,而无需其他改动。

修改 这个答案告诉你如何捕获事件并使其工作,这就是问题所在。 FollowHyperlink的问题是第二件事:MSDN表示该方法将根据您传递的目标“打开相应的程序”。

由于代码正确事件触发,这很可能是一个完全不同的问题,与VBA /您的问题无关。

在我的机器上(W7 + Excel 2016),代码执行完美,并且链接上的点击始终如一。