在用户表单中插入超链接 VBA
Inserting a Hyperlink in Userform VBA
我使用用户窗体在 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
此代码段中出现问题:
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 表示该方法将 "open the appropriate program" 基于您传递的目标。
由于代码正确并且事件触发,这很可能是一个完全不同的问题,与VBA / 你的问题。
在我的机器上(W7 + Excel 2016)代码执行完美,点击 link 始终如一。
我使用用户窗体在 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
此代码段中出现问题:
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 表示该方法将 "open the appropriate program" 基于您传递的目标。
由于代码正确并且事件触发,这很可能是一个完全不同的问题,与VBA / 你的问题。
在我的机器上(W7 + Excel 2016)代码执行完美,点击 link 始终如一。