2015-10-12 19 views
0

我想創建一個宏,它在運行時將包含在剪貼板中的鏈接粘貼到當前單元格作爲超鏈接。我試圖生成下面的代碼,我修改了一點錄製宏:在剪貼板中添加文本作爲指向單元格的超鏈接

Sub Macro1() 
' 
' Macro1 Macro 
' 

' 
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="abc.com" _ 
     , TextToDisplay:="Link" 
End Sub 

在這裏,而不是abc.com應該是這樣的「鍵盤粘貼文本」。

+0

http://stackoverflow.com/questions/9022245/get-text-from-clipboard-using-gettext-avoid-錯誤的空剪貼板 – DanL

回答

0

按照@DanL評論,這裏是代碼,您需要:

Sub Macro1() 
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=GetClipboardText() _ 
     , TextToDisplay:="Link" 
End Sub 

Function GetClipBoardText() as String 
    Dim DataObj As MSForms.DataObject 
    Set DataObj = New MsForms.DataObject '<~~ Amended as per jp's suggestion 

    On Error GoTo Whoa 

    '~~> Get data from the clipboard. 
    DataObj.GetFromClipboard 

    '~~> Get clipboard contents 
    GetClipBoardText = DataObj.GetText(1) 


    Exit Sub 
Whoa: 
    If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty" 
End Sub 
相關問題