I have the following piece of code that copies and pastes pictures from the worksheet 'RefData' to another worksheet called 'Dashboard' based on a value in a column H/L on the 'Dashboard' worksheet. It has been working fine for a good couple of years but recently when I run it, it runs but then immediately throws me out of excel. If I step through it, it works fine. Any help gratefully received. Please be kind, I am not an expert user.
Public Sub UpdatePictures()
Dim IconRefresh As Variant
Sheets("Dashboard").Select
If ActiveSheet.Pictures.Count > 1 Then
ActiveSheet.Shapes.SelectAll
Selection.Delete
MsgBox "Pictures Deleted"
Else
MsgBox "No Pictures To Delete"
End If
Sheets("RefData").Select
ActiveSheet.Shapes.Range(Array("Common")).Select
Selection.Copy
Sheets("Dashboard").Select
For Each Cell In Range("H6:H15")
If Cell.Value = "Common" Then
Cell.Offset(0, 20).Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 15
Selection.ShapeRange.IncrementTop 3.5
End If
Next
Sheets("RefData").Select
ActiveSheet.Shapes.Range(Array("HighSpecial(Concern)")).Select
Selection.Copy
Sheets("Dashboard").Select
For Each Cell In Range("H6:H15")
If Cell.Value = "HighSpecial(Concern)" Then
Cell.Offset(0, 20).Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 15
Selection.ShapeRange.IncrementTop 3.5
End If
Next
Sheets("RefData").Select
ActiveSheet.Shapes.Range(Array("Pass")).Select
Selection.Copy
Sheets("Dashboard").Select
For Each Cell In Range("L6:L15")
If Cell.Value = "Pass" Then
Cell.Offset(0, 19).Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 15
Selection.ShapeRange.IncrementTop 3.5
End If
Next
Sheets("RefData").Select
ActiveSheet.Shapes.Range(Array("Fail")).Select
Selection.Copy
Sheets("Dashboard").Select
For Each Cell In Range("L6:L15")
If Cell.Value = "Fail" Then
Cell.Offset(0, 19).Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 15
Selection.ShapeRange.IncrementTop 3.5
End If
Next
Sheets("RefData").Select
Sheets("Dashboard").Select
Range("AA5").Select
MsgBox "Pictures Updated"
End Sub
I have googled it and have come across others having similar issues, I've checked the answers but don't quite have the VBA skills to know how to fix mine
I have definitely noticed that copy/paste in Excel has become pretty flakey in the past few years, particularly when in a loop and pictures/shapes are involved.
Try this out - a little refactored, and using a separate sub to perform the copy/paste, with re-tries if it fails: