如何将单元格的内容复制到剪贴板 [英] How do you copy the contents of a Cell into the Clipboard
问题描述
我正在尝试将单元格内容复制到剪贴板中.
I'm trying to copy the cell contents into the clipboard.
我已阅读并尝试了Excel 2007帮助文件中提供的确切示例.但是由于某种原因,DataObject对象无效.因此,示例:
I've read and tried the exact example provided in the Excel 2007 Help file. However for some reason the DataObject object is not valid. So the example:
Dim MyData As DataObject
Private Sub CommandButton1_Click()
Set MyData = New DataObject
MyData.SetText TextBox1.Text
MyData.PutInClipboard
TextBox2.Paste
End Sub
Private Sub UserForm_Initialize()
TextBox1.Text = "Move this data to a " _
& "DataObject, to the Clipboard, then to " _
& "TextBox2!"
End Sub
在我的情况下不起作用.我已经搜索了好一阵子,但找不到为什么DataObject对象不可用的答案.
Does not work in my case. I've searched for a good while now and I can not find an answer to why the DataObject object is not available.
这是我的代码:
Dim MyData As DataObject
Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveCell.Column = 3 Then
Set MyData = New DataObject
MyData.SetText ActiveCell.Offset(-1, -1).Text
MyData.PutInclipboard
End If
End Sub
编译时的错误是:未定义用户定义的类型",并突出显示了"MyData As DataObject"行.
Error on Compile is: "User-Defined type not defined" and it highlights the "MyData As DataObject" line.
还有另一种方法可以简单地将单元格中的文本复制到剪贴板吗?
推荐答案
好,几件事:
首先,您需要添加对"Microsoft Forms 2.0对象库"的引用,如果您不想进行搜索,只需将用户表单添加到项目中,然后立即将其删除,该引用将保留.您需要该引用才能使用DataObject
First you need to Add a reference to "Microsoft Forms 2.0 Object Library", if you don't feel like searching through, just add a user form to the project and then immediately delete it, the reference will stay. You need that reference to use DataObject
我将附上一些我需要工作的代码,我更改了偏移量,但可以根据需要设置它们……
I'll attach some code that I got to work, I changed the offsets but make them whatever you need...
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Set MyData = New DataObject
If ActiveCell.Column = 3 Then
MyData.SetText ActiveCell.Offset(-1, 0).Text
MyData.PutInClipboard
End If
End Sub
这篇关于如何将单元格的内容复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!