我正在Excel下编写一个程序,通过将一些数据从一个工作表复制/粘贴到另一个工作表来生成大量电子表格(例如:使用带有页眉/页脚单元格的“布局”工作表,这些单元格将被复制/粘贴到生成的工作表上)。
我的问题是,有时(不是每次),在运行我的“生成过程”时,Excel会生成这个错误(抱歉,这是我法语Excel错误的英文翻译):
错误1004:“_Worksheet”对象的“粘贴”方法失败
因此,我假设剪贴板有问题(我电脑上的其他软件可能同时使用剪贴板:/)
首先,我试图找到一种不使用剪贴板复制/粘贴我的单元格(和其他东西)的方法,代码如下:
ThisWorkbook.Sheets("Layout").Range("A1").Copy Destination:=ThisWorkbook.Sheets("Test").Range("A1")
或者是那个
ThisWorkbook.Sheets("Test").Range("A1") = ThisWorkbook.Sheets("Layout").Range("A1")
但似乎我们只能复制文本或公式,而不是所有的东西(边框,颜色,.)也不是图表对象(我有)!
因此,我试图在复制/粘贴过程中找到锁定/解锁剪贴板的方法。我找到了这样的代码:
Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function EmptyClipboard Lib "user32" () As Long
Public Sub Lockk()
If OpenClipboard(0) = 0 Then
MsgBox "cannot open clipboard."
ElseIf EmptyClipboard() = 0 Then
MsgBox "cannot clear clipboard."
End If
End Sub
Public Sub Unlockk()
CloseClipboard
End Sub
在复制单元格时,它似乎是有效的:我可以锁定excel下的剪贴板,转到另一个软件(例如记事本),并且无法将一些数据复制到这个软件中;返回excel,我可以复制/粘贴数据(手动或宏)。
但:
那么,有人想知道如何锁定/解锁剪贴板来复制图表对象吗?或者不使用剪贴板就复制它们?
编辑:
用于复制/粘贴图形对象的代码:
Utils_Clipboard.Lockk
ThisWorkbook.Sheets("Layout").ChartObjects("CHART_TEMPLATE").Copy
DoEvents
worksheet_p.Paste Destination:=where_p
Utils_Clipboard.Unlockk
其中worksheet_p是工作表对象,where_p是范围。请注意,如果没有第一行和最后一行(锁定剪贴板),它可以正常工作(除了一些时间)。
发布于 2013-01-17 04:21:57
好的,我找到了一个解决办法(也许不是最好的?)
我可以使用我的Utils_Clipboard.Lockk和Utils_Clipboard.Unlockk来确保剪贴板在复制(复制/粘贴)单元格、合并单元格、.
但是,当剪贴板被锁定时,我们似乎无法复制/粘贴图表对象(手动按ctrl+c和ctrl+v键;或者在vba中自动复制对象的metod副本和过去)。
我对图表对象的解决方案是像这样使用函数复制(http://msdn.microsoft.com/en-us/library/office/ff840956.aspx)和移动(http://msdn.microsoft.com/en-us/library/office/ff840583.aspx) (其中worksheet_p是我想要放置chartobject的工作表):
Utils_Clipboard.Lockk
Dim newchart_l As Shape
Set newchart_l = ThisWorkbook.Sheets("Layout").ChartObjects("CHART_TEMPLATE").Duplicate
newchart_l.Chart.Location xlLocationAsObject, worksheet_p.Name
Utils_Clipboard.Unlockk
请注意,复制的对象是形状,而不是ChartObject (如果我们键入为ChartObject,这将在执行代码时产生错误)。
所以,它可以工作(我认为这里不需要锁定剪贴板),但是图表对象不是我想要的地方(在worksheet_p上正确的上/左坐标处)。为此,我发现我们必须移动ChartArea (复制对象的父对象),但是我们不能直接操作"newchart_l“(似乎在重复调用之后,Excel不会更新其所有内部变量,为什么??)。因此,我的解决方案是首先检索新的重复图表对象,如下所示:
Dim ChartObject_m As Chart
Set ChartObject_m = worksheet_p.ChartObjects(worksheet_p.ChartObjects.Count).Chart
然后移动该对象的图表区域(其中'where_p‘是我要放置字符对象的区域/单元格):
ChartObject_m.ChartArea.Left = where_p.Left
ChartObject_m.ChartArea.Top = where_p.Top
看哪!
发布于 2014-12-25 02:04:18
在您的解决方案的启发下,我有了一段经过改进的代码,我想要共享它。改进之处在于,它不依赖诸如“ChartObjects-Collection反映插入顺序的图表顺序”这样的假设:
Private Function copyGraph(source As ChartObject, pos As Range) As ChartObject
' Copies a given graph to the given position and returns the resulting
' ChartObject. The destination position is expected to be a cell in the desired
' target worksheet. The resulting ChartObject will be aligned to the Top/Left-
' Border of the given cell.
Dim dup As Object
Dim dstChart As Chart
' First just duplicate the graph. This operation leaves it on the original
' worksheet.
Set dup = source.Duplicate
' In case the duplication failed, ...
If (Not dup.HasChart) Then
' ... we remove the duplicated object and leave the copy function.
' This yields a Nothing-reference as return value to signal the error
' to the caller.
dup.Delete
set copyGraph = Nothing
Exit Function
End If
' Then we move the graph to the target worksheet passed as parameter. This
' gives us the new link to the moved chart.
'
' Excel displays some weired behavior when using the reference returned by ChartObject.Duplicate.
' Namely it yields sporadic 1004 runtime errors without further specification. However it seems,
' that activating the chart and calling location for ActiveChart gets around this problem.
'
' Therefor the original code:
' Set dstChart = dup.Chart.Location(xlLocationAsObject, pos.Parent.Name)
' has been replaced with the following
dup.Chart.parent.Activate
Set dstChart = ActiveChart.Location(xlLocationAsObject, pos.Parent.Name)
' As we relocated the chart as an object, the parent of the chart object is
' an instance of ChartObject. Hence we use it as the return value.
Set copyGraph = dstChart.parent
' Finally we move the graph to the requested position passed by the pos
' parameter.
With copyGraph
.Top = pos.Top
.Left = pos.Left
End With
End Function
我希望这能帮助其他用户在这个问题上寻找一个简单的解决方案。
https://stackoverflow.com/questions/14362260
复制相似问题