简体   繁体   English

如何修复在Excel 2010中起作用的Excel 2016对象必需错误

[英]How to fix Excel 2016 Object Required Error that worked in Excel 2010

I have recently migrated to Excel 2016 from 2010 and one of the sheets I inherited from a colleague has stopped working. 我最近从2010年迁移到Excel 2016,从同事那里继承的一张工作表已停止工作。 I am in the process of learning VBA but would appreciate help with the run-time error that I keep getting when running the below code. 我正在学习VBA,但希望获得有关运行以下代码时遇到的运行时错误的帮助。

I believe it results from the way variables are declared (or seemingly not in this case). 我相信这是由声明变量的方式导致的(或者在这种情况下似乎不是)。 The function takes in a range which overlays a chart. 该函数采用覆盖图表的范围。 It then exports the chart as a .png image. 然后将图表导出为.png图像。 Another thing I don't understand is why it works fine in Excel 2010 but not 2016? 我不明白的另一件事是,为什么它在Excel 2010中能正常工作,但在2016年中却不能工作?

Error 424 - Object Required: 错误424-所需对象:

错误424-必需的对象

The line the error occurs : 发生错误的行

 With .Pictures(1)

The code: 编码:

Sub createPNG(sheetName As String, rangeName As String, fileName As String)

  Dim vFilePath As Variant
  Dim rSelection As Range
  Dim sDefaultName As String

  Sheets(sheetName).Range(rangeName).Select

  Set rSelection = Selection

  vFilePath = "Z:\marginsOutput\Charts\" & fileName & ".png"
  '-- copy selected range as picture (not as bitmap)
  rSelection.CopyPicture Appearance:=xlScreen, Format:=xlPicture

  '--Create an empty chart, slightly larger than exact size of range copied
  With Sheets(sheetName).ChartObjects.Add( _
      Left:=rSelection.Left, Top:=rSelection.Top, _
      Width:=rSelection.Width + 2, Height:=rSelection.Height + 2)

  With .Chart
  ' clean up chart
  .ChartArea.Format.Line.Visible = msoFalse

  ' paste and position picture
  .Paste
  With .Pictures(1)
    .Left = .Left + 2
    .Top = .Top + 2
  End With

  ' export
  .Export CStr(vFilePath)
End With

' remove no-longer-needed chart
.Delete
  End With

End Sub

I find sometimes in VBA the .paste just doesn't seem to work the first time around especially if the item is already in the clipboard! 我发现有时在VBA中,.paste似乎第一次无法工作,特别是如果该项目已经在剪贴板中! I've added a few lines to check if there is an picture present before trying to select the picture! 在尝试选择图片之前,我添加了几行以检查是否存在图片! if it's not found then it will attempt the .paste again. 如果找不到,它将再次尝试.paste。 you should add some control to this to stop it looping on paste errors! 您应该为此添加一些控件以阻止它在粘贴错误时循环!

try the edit below; 尝试下面的编辑;

Sub createPNG(sheetName As String, rangeName As String, fileName As String)

  Dim vFilePath As Variant
  Dim rSelection As Range
  Dim sDefaultName As String

  Sheets(sheetName).Range(rangeName).Select

  Set rSelection = Selection

  vFilePath = "Z:\marginsOutput\Charts\" & fileName & ".png"

  '-- copy selected range as picture (not as bitmap)
  rSelection.CopyPicture Appearance:=xlScreen, Format:=xlPicture

  '--Create an empty chart, slightly larger than exact size of range copied
  With Sheets(sheetName).chartobjects.Add( _
      Left:=rSelection.Left, Top:=rSelection.Top, _
      Width:=rSelection.Width + 2, Height:=rSelection.Height + 2)

  With .Chart
  ' clean up chart
  .ChartArea.Format.Line.Visible = msoFalse

  ' paste and position picture
  .Paste
  If .Pictures.Count = 0 Then
        .Paste
  End If
  With .Pictures(1)
    .Left = .Left + 2
    .Top = .Top + 2
  End With

  ' export
  .Export CStr(vFilePath)
End With

' remove no-longer-needed chart
.Delete
  End With

End Sub

It works, if you just select the ChartObject before pasting a picture into its Chart . 如果仅在将图片粘贴到其Chart之前选择ChartObject ,它将起作用。
This is one of the seldom cases, where a .Select helps - otherwise see How to avoid using Select in Excel VBA 这是很少的情况下,如果一个.Select帮助-否则看看如何避免在Excel中使用VBA选择

With Sheets(sheetName).ChartObjects.Add( _
    Left:=rSelection.Left, Top:=rSelection.Top, _
    Width:=rSelection.Width + 2, Height:=rSelection.Height + 2)
    .Select
    With .Chart
        ' ...
    End With
End With

... and during debugging, it is really annoying: ...并且在调试过程中,这确实很烦人:
If you run the code step-by-step via (F8), it works without ChartObject.Select , but if you run it normally or via (F5) you get an error, either 如果您通过(F8)逐步运行代码,则该代码可以在没有ChartObject.Select情况下运行,但是如果您正常运行或通过(F5)运行代码,则会出现错误,
Picture(1) = Error 424 (no such object) Picture(1)=错误424(无此类对象)
Shapes(1) = Error -2147024809 (number 1 not there) Shapes(1)=错误-2147024809(数字1不存在)

Like Max, I have found that many commands in Excel that operate on various objects (shapes, charts, pictures, worksheets, workbooks, etc.) take longer than they used to, and VBA doesn't wait for them to finish before trying to execute the next command. 像Max一样,我发现Excel中许多对各种对象(形状,图表,图片,工作表,工作簿等)进行操作的命令所花费的时间比以前更长,并且VBA不会等待它们完成才尝试执行下一条命令。 Hence the failure when it tries doing something to the picture which isn't really pasted yet. 因此,当尝试对尚未真正粘贴的图片执行操作时,将导致失败。

An indication of this problem is that the code runs without error when stepping through in the VB Editor with the F8 key, but fails at full speed when running with the F5 key. 此问题的迹象是,在使用F8键在VB编辑器中单步执行时,代码运行时没有错误,但在使用F5键运行时,全速失败。

Similar to Max, I've come up with a little loop to watch for this: 与Max类似,我想出了一个小循环来注意这一点:

With .chart
  Do Until .Pictures.Count = 1
    DoEvents
    .Paste
  Loop
End With

An alternative is to place the actual Paste in a separate function, which you would call from that part of the code, and pass in the picture being pasted and the chart that is receiving it. 另一种选择是将实际的Paste放置在单独的函数中,您可以从代码的那部分调用该函数,然后传递要粘贴的图片和接收该图片的图表。 The barrier between different items in the call stack seems to encourage VBA to finish what it's doing before going on the the next step. 调用堆栈中不同项目之间的障碍似乎鼓励VBA在继续下一步之前完成其工作。

I should note that I've had to do this to many parts of my commercial Excel VBA add-in that works extensively with charts, and I the code I pasted above is taken directly out of this add-in. 我应该注意,我必须对广泛使用图表的商业Excel VBA加载项的许多部分执行此操作,而我上面粘贴的代码直接从该加载项中删除。

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

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