VBA Paste Special Method of Range Class Failed - Intermittent occurence

Brent 6 Reputation points
2022-07-01T19:05:23.163+00:00

I've written code to read in a data file, create a plot, and then copy the plot and paste it as a picture onto another worksheet in the same workbook. The code does this for multiple data files.

Below is the portion of the code where the problem occurs:

Sheets("RawData").Activate
ActiveSheet.ChartObjects("Chart 7").Select
ActiveChart.ChartArea.Copy

Worksheets(LogSheetName).Activate
Range("A1").Activate
Selection.Offset(27 * (ifile - 1), 0).Select
ActiveSheet.PasteSpecial format:="Picture (JPEG)", Link:=False, _
DisplayAsIcon:=False

The problem is that intermittently I get an error: Paste Special Method of Range Class Failed. It fails on the last line. If I re-run the code with the same set of data files it will fail a different number of times and the files it fails on will be different. So, the actual data file does not matter. It is a problem that occurs randomly. When an error occurs I can click debug and then the run arrow to get it going again - and then the code goes on to execute everything correctly. This is the only way I can effectively process all the files I need to get through. But it means I have to babysit the code until it is finished.

I am out of things to try

{count} vote