2017-06-28 54 views
-1

我在excel中使用用戶窗體創建了一個歡迎筆記,每當我打開工作簿時,該窗體每次顯示6秒。我在該用戶表單上插入了一個超鏈接。但是,超鏈接不起作用。 我用下面的代碼在標籤..在Userform VBA中插入超鏈接

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

+0

究竟什麼不行? 這裏罰款。 –

+0

超鏈接無法正常工作。每當我點擊超鏈接,網站都沒有打開。 – omprakash

+0

您是否嘗試過使用F8執行,以查看上述事件是否被解僱?您是否收到MsgBox說「無法打開https ......」? –

回答

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),代碼執行完美,點擊鏈接一直工作。

+0

謝謝你Rik..Atlast它的工作 – omprakash

+0

不客氣的隊友。另外 - 如果你可以把你的附加代碼放在你的問題中,並刪除代碼註釋,那會很棒。否則,我會標記這一個適度。 –

+0

另一個問題隊友..有時鏈接正在打開,但有時候消息框正在彈出與無法打開網站....怎麼辦 – omprakash