vba works if stepping but not playing - at Chart.Paste

mtharnden

Board Regular
Joined
Aug 17, 2011
Messages
114
Hello,

I have a code that works perfectly every time when I step through it, however when I play it normally it does not. It will save a white image rather than the range.
This code saves a range as a .PNG. It took me forever to find one that worked the way I want it to. What I like about this method is it works zooming in and saving the file as a large image as I am displaying them on large TV displays.

I think it is the chart.paste piece that messes up. If a play up to that then step it works, if I play just past it then step it does not work.

I have tried different methods of this that have not worked in the way I like.

Code:
Sub test2()


    Set Sheet = Worksheets("test_sheet")
    Sheets("test_sheet").Select
    output = "C:\Users\matthew.harnden\Documents\test_image.png"
    zoom_coef = 100 / Sheet.Parent.Windows(1).Zoom * 4
    Set area = Worksheets("test_sheet").Range("B7:O30")
    area.CopyPicture xlBitmap
    Set chartobj = Sheet.ChartObjects.Add(0, 0, area.Width * zoom_coef, area.Height * zoom_coef)
    chartobj.Chart.Paste
    chartobj.Chart.Export output, "png"
    chartobj.Delete


End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
After you've created your chartobject and assigned it to chartobj, try activating the chartobject before exporting it...

Code:
chartobj.Activate
 
Upvote 0
After you've created your chartobject and assigned it to chartobj, try activating the chartobject before exporting it...

Code:
chartobj.Activate


sir, you are amazing! i placed that before the paste and it worked. thank you very much!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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