将活动单元格的行号复制到剪贴板(Excel VBA)



我想创建一个宏,该宏获取活动单元格的行号并将其复制到剪贴板。

当我执行以下代码时,我得到"编译错误:无效的限定符"。

我已经忘记了我的大部分 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

你可以看看这个答案。由于使用后期绑定,因此无需添加引用。

您的代码看起来像这样(私有子是从我上面链接到的答案中复制的(:

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

最新更新