Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将剪贴板中的文本作为超链接添加到单元格_Vba_Excel - Fatal编程技术网

Vba 将剪贴板中的文本作为超链接添加到单元格

Vba 将剪贴板中的文本作为超链接添加到单元格,vba,excel,Vba,Excel,我想创建一个宏,运行该宏时,将剪贴板中包含的链接作为超链接粘贴到当前单元格。我尝试使用record宏生成了以下代码,我对其进行了一些修改: Sub Macro1() ' ' Macro1 Macro ' ' ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="abc.com" _ , TextToDisplay:="Link" End Sub 在这里,它应该是类似于在键盘中粘贴文本的内容,而不是abc.com

我想创建一个宏,运行该宏时,将剪贴板中包含的链接作为超链接粘贴到当前单元格。我尝试使用record宏生成了以下代码,我对其进行了一些修改:

Sub Macro1()
'
' Macro1 Macro
'

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

在这里,它应该是类似于在键盘中粘贴文本的内容,而不是abc.com。

根据@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