作者:忆--______100 | 来源:互联网 | 2023-08-01 12:05
我想创建一个使用活动单元格行号的宏并将其复制到剪贴板。
执行以下代码时,出现“编译错误:无效的限定符”。
我已经忘记了我的大部分VBA,但是可以肯定,这种简单的方法应该可以工作吗?预先感谢您提供的任何帮助。
Sub macro3()
Dim x As Integer
x = activeCell.row
x.Copy
End Sub
编辑:
使用以下解决方案,我将其更改为以下内容(有效):
首先在VBE中的“工具”>“引用”下添加对microsoft Forms 2.0对象库的引用:
Sub macro3()
Dim x As DataObject
Set x = New DataObject
x.settext activeCell.row
x.putinclipboard
End Sub
使用DataObject
的一个选项-在VBE中的工具> 参考下添加对Microsoft Forms 2.0 Object Library
的引用。
Sub Test()
Dim x As DataObject
Set x = New DataObject
x.SetText ActiveCell.Row 'Or CStr(ActiveCell.Row) to make the type conversion explicit
x.PutInClipboard
End Sub
,
您可以看看这个answer。由于使用了后期绑定,因此您无需添加引用。
您的代码应类似于(从我上面链接的答案中复制的私人子代码):
Sub RowNumberToClipboard()
Dim lRow As Long
lRow = ActiveCell.Row
Call CopyText (CStr(lRow))
End Sub
Private Sub CopyText(Text As String)
'VBA Macro using late binding to copy text to clipboard.
'By Justin Kay,8/15/2014
Dim MSForms_DataObject As Object
Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
MSForms_DataObject.SetText Text
MSForms_DataObject.PutInClipboard
Set MSForms_DataObject = Nothing
End Sub