I had been running a cut and paste picture routine for some time and all of a sudden Excel starting giving me this run time error. It had been working fine for several days until now (no OS update or reboot, though I did try closing and reopening Excel to see if it helped). Stranger still, the script does a batch copy and paste.picture, the same range (with recalculated values) is copied and pasted 13 times and the error message pops up usually in the last loop or occasionally at some random point.
I looked up support.microsoft.com/en-us/kb/905164: "This issue may occur if either of the following conditions is true:
The Microsoft Visual Basic for Applications (VBA) macro copies and pastes one whole row in an Excel 2003 workbook.
The Microsoft VBA macro copies and pastes a range of 2,516 rows or more rows in an Excel 2003 workbook."
However, I am copying a range of 12,12 cells, from A1 to L12 to be exact, not even close to an entire row. I have tried using range.offset, xldown, rannge(cells(1,1), cells(12,12)) but none of these helped.
Has anyone experienced something similar?
Sub PutPic(ByRef FN As String)
Dim fname As String
fname = "E:\Users\ABCD\Documents\EFGH\" & FN
Worksheets(2).Range(Cells(1, 1), Cells(12, 12)).Select
'Sheets("sheet2").Range("A1:l12").Select
Selection.Copy
'Sheets("sheet2").Range("a1").Select
ActiveSheet.Pictures.Paste(Link:=False).Select
Selection.Name = "Pic"
Selection.ShapeRange.ScaleWidth 2, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 2, msoFalse, msoScaleFromMiddle
Dim ChtObj As ChartObject
With ThisWorkbook.Worksheets(2)
.Activate
Set ChtObj = .ChartObjects.Add(100, 100, 400, 400)
ChtObj.Name = "PicFrame"
ChtObj.Width = .Shapes("Pic").Width
ChtObj.Height = .Shapes("Pic").Height
ActiveSheet.Shapes.Range(Array("Pic")).Select
Selection.Copy
ActiveSheet.ChartObjects("PicFrame").Activate
ActiveChart.Paste
ActiveChart.Export Filename:=fname, FilterName:="png"
ChtObj.Delete
ActiveSheet.Shapes.Range(Array("Pic")).Delete
End With
End Sub
The sub with the looping routine, totally ordinary which feeds a filename to the subroutine.
Public Sub MainRun()
Dim i, j, k As Long
Dim NMG, NMB As String
Dim FNGBSig As String
Dim FNUnivSig As String
Dim BatchStart, Batch As Long
BatchStart = ThisWorkbook.Worksheets(2).Cells(15, 1).Value + 1
Batch = 13
For i = BatchStart To BatchStart + Batch - 1
'Some calculations that refresh values in range A1:L12
FNGBSig = i & "GoodBad.png"
PutPic FNGBSig
Next i
End Sub