Excel 2016 Macro will not work correctly

bergy

Board Regular
Joined
Apr 23, 2002
Messages
115
I have a macro I have been using for years where a range of cells is copied using the CopyPicture method. The macro then creates a chart object, pastes the picture into the chart, then exports the chart as a jpg. Finally, the chart is deleted. This has worked with Excel 2010 just fine, but now gives me an empty jpg file with Excel 2016.

Here's the code that works:
With ActiveSheet
Set r = .Range(Cells(19, 3), Cells(28, 12))
r.CopyPicture Appearance:=xlScreen, Format:=xlBitmap

Set chtO = .ChartObjects.Add(1, 1, r.Width, r.Height)
End With

Here's the part that doesn't:

With chtO
Chart.Paste
Chart.Export FileName:=FileName, FilterName:="JPG" ' file name is created prior to this code
.Delete
' End With

To be more specific, the .Paste doesn't work. The chart is exported (though empty) and the chart is deleted.
One more thing, If I step through the code, it works. Any ideas?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,181
Office Version
  1. 365
Platform
  1. Windows
I don't see where you set the value of filename for the input filename.
 
Upvote 0

bergy

Board Regular
Joined
Apr 23, 2002
Messages
115
It is set earlier in the code. I know that line works because I get a new file, but it is empty. I just now added a new line of code in the With chtO section of code:

.Select

This seems to work.
 
Upvote 0

Forum statistics

Threads
1,191,579
Messages
5,987,425
Members
440,096
Latest member
yanaungmyint

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
Top