我想创建一个宏,该宏在运行时将剪贴板中包含的链接粘贴为当前单元格的超链接。我尝试使用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句