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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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