简体   繁体   English

Excel VBA-将带有图表的工作表复制到另一个工作簿,关闭时出错

[英]Excel VBA - copying sheet with charts to another workbook, error on close

I got stuck with this issue: 我陷入了这个问题:

I am copying a number of sheets from several Excel workbooks into one specific workbook. 我正在将多个工作表从多个Excel工作簿复制到一个特定的工作簿中。 I use this code: 我使用以下代码:

SourceWorkbook.SourceSheet.Cells.Copy DestinationWorkbook.DestinationSheet.Cells(1,1)

DestinationWorkbook being a Object with loaded Excel workbook. DestinationWorkbook是带有已加载Excel工作簿的对象。

Everything works fine until I ran into sheets with charts (chartobjects). 一切正常,直到我遇到带有图表(图表对象)的工作表。 Copying is fine, but I am unable to close DestinationWorkbook and I receive a error message about insufficient resources for the action (memory?). 复制很好,但是我无法关闭DestinationWorkbook,并且收到有关该操作的资源不足(内存?)的错误消息。

I think it might be something with links (charts are linked to the original workbook). 我认为可能是带有链接的东西(图表已链接到原始工作簿)。

I have tried to clear clipboard, change .Values, .XValues, .Names of all charts, break links (not sure it worked properly), save the DestinationWorkbook, DoEvents, On Error Resume Next, etc. but it still doesnt work properly. 我试图清除剪贴板,更改所有图表的.Values,.XValues,.Name,断开链接(不确定它是否正常工作),保存DestinationWorkbook,DoEvents,On Error Resume Next等,但仍然无法正常工作。

My last option is to delete the charts from SourceWorkbook and create them by myself but I would like to avoid it, as it will be much complicated as it may vary from sheet to sheet. 我的最后一个选择是从SourceWorkbook中删除图表并自己创建它们,但是我想避免这样做,因为它会非常复杂,因为每个工作表之间可能会有所不同。

Hope anyone can help. 希望任何人都能提供帮助。

The whole code is complicated but the most important part is this: 整个代码很复杂,但是最重要的部分是:

Set SourceWorkbook = GetObject(Path)
SourceWorkbook.SourceSheet.Cells.Copy DestinationWorkbook.DestinationSheet.Cells(1,1)
SourceWorkbook .Close Savechanges:=False
Set SourceWorkbook = Nothing

Instead of copying the entire sheet which may cause memory leek: 而不是复制可能导致内存溢出的整个工作表:

SourceWorkbook.SourceSheet.Cells.Copy DestinationWorkbook.DestinationSheet.Cells(1,1)

Try copying a specific range: 尝试复制特定范围:

  SourceWorkbook.Sheets(SourceSheet).Range(SourceWorkbook.Sheets(SourceSheet).Cells(1, 1), _
  SourceWorkbook.Sheets(SourceSheet).Cells(100, 100)).Copy ThisWorkbook.Sheets(DestinationSheet).Cells(1, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM