[VBA] Run Time Error

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
21,761
Office Version
  1. 365
Platform
  1. Windows
I have a macro that moves a picture within a cell to a comment within the same cell. It does this for all pictures within the active sheet. In Excel 2013, the macro runs fine. However, in Excel 2016, if I step through the code manually, it works fine. But if I run it I get the following run-time error...

Code:
Run-time error:  '424':

Object required

...which occurs at the following line...

Code:
.Chart.Pictures(1).Delete

When I step through it manually and it gets to the above line, the picture exists within the chart. So I don't get an error. But when I run the macro and it gets to the above line, the picture doesn't exist for some reason, hence the error. Does anyone know why this is the case? Am I overlooking something? Is this some sort of bug?

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] MovePicturesFromCellIntoComments()

    [COLOR=darkblue]Dim[/COLOR] oShp [COLOR=darkblue]As[/COLOR] Shape
    [COLOR=darkblue]Dim[/COLOR] sSaveAsFilename [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    [COLOR=darkblue]Const[/COLOR] ZoomFactor As [COLOR=darkblue]Double[/COLOR] = 2.5 [COLOR=green]'two and a half times larger[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] ActiveSheet.ChartObjects.Add(Left:=0, Top:=0, Width:=250, Height:=250)
        .Border.LineStyle = 0
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] oShp [COLOR=darkblue]In[/COLOR] ActiveSheet.Shapes
            [COLOR=darkblue]If[/COLOR] oShp.Type = msoPicture [COLOR=darkblue]Then[/COLOR]
                sSaveAsFilename = Environ("temp") & "\" & oShp.Name & ".jpg"
                oShp.Copy
                [COLOR=darkblue]With[/COLOR] .Chart
                    .ChartArea.Width = oShp.Width
                    .ChartArea.Height = oShp.Height
                    .Paste
                    .Export sSaveAsFilename
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                [COLOR=darkblue]With[/COLOR] oShp.TopLeftCell.AddComment
                    .Text Text:=""
                    [COLOR=darkblue]With[/COLOR] .Shape
                        .Width = oShp.Width * ZoomFactor
                        .Height = oShp.Height * ZoomFactor
                        .Fill.UserPicture sSaveAsFilename
                    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
                .Chart.Pictures(1).Delete
                oShp.Delete
                Kill sSaveAsFilename
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]Next[/COLOR] oShp
        .Delete
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
A guess, but your first With object is:

Rich (BB code):
ActiveSheet.ChartObjects.Add(Left:=0, Top:=0, Width:=250, Height:=250)

Which makes the error occur on:

Rich (BB code):
ActiveSheet.ChartObjects.Add(Left:=0, Top:=0, Width:=250, Height:=250).Chart.Pictures(1).Delete


It could be it's the wrong attribute or object that you're trying to delete, in relation to the original With object? Maybe incorrect syntax?
 
Last edited:
Upvote 0
Jack, thanks for looking into this, but it doesn't appear to be the cause. When I try running a simple code that creates a chart object, copies a shape or picture, and pastes it into the chart, the shape or picture doesn't actually get pasted. Although, it does get pasted when I step through it manually. Here's the simple test that I tried...

Code:
[COLOR=darkblue]Sub[/COLOR] test()
    
    [COLOR=darkblue]Set[/COLOR] oChart = ActiveSheet.ChartObjects.Add(Left:=0, Top:=0, Width:=250, Height:=250).Chart
        
    ActiveSheet.Shapes(1).Copy

    oChart.Paste
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

As I mentioned, this only occurs in Excel 2016. It works fine in Excel 2013.

Any other thoughts?
 
Last edited:
Upvote 0
Try to insert some dummy Msgbox or Debug.Print before pasting. Or insert Windows API Sleep function call:
Code:
Public Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)
 
Upvote 0
Thanks Sektor. I tried all three of your suggestions, but the same thing happens. The shape or picture doesn't get pasted into the chart. It must be some sort of bug. Although, I tried searching Google, but didn't find anything regarding this issue.

Any other thoughts?
 
Upvote 0
Yes, this is really strange. I've figured out that right after pasting (.Paste) .... there's no picture! MsgBox ch.Pictures.Count shows 0! Although if to go step-by-step, MsgBox shows 1.
 
Upvote 0
I have been playing with this for a while and it doesn't seem to know that it has done all the pictures so goes looking for 1 more and hence the runtime error object required
 
Upvote 0
I have been playing with this for a while and it doesn't seem to know that it has done all the pictures so goes looking for 1 more and hence the runtime error object required

Actually, if you test the code in Post #3 in this thread you'll see that a simple copy/paste into the chart results in no picture being pasted. Note, this only occurs in Excel 2016. It works fine in Excel 2013.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top