The basic/core code is as follows:
{
On Error Resume Next
Set Shape = ActiveSheet.Shapes("Picture 1")
On Error GoTo 0
If Shape Is Nothing Then
Exit Sub
End If
If Range("Q4").Value < 0.025 Then
ActiveSheet.Shapes("Picture 1").Select
Selection.Cut
End If
}
I am trying to create 50-100 instances of this code where each instance changes a bit. For example, the 2nd code instance must be like this:
{
On Error Resume Next
Set Shape = ActiveSheet.Shapes("Picture 2")
On Error GoTo 0
If Shape Is Nothing Then
Exit Sub
End If
If Range("Q5").Value < 0.025 Then
ActiveSheet.Shapes("Picture 2").Select
Selection.Cut
End If
}
... and the 3rd:
{
On Error Resume Next
Set Shape = ActiveSheet.Shapes("Picture 3")
On Error GoTo 0
If Shape Is Nothing Then
Exit Sub
End If
If Range("Q6").Value < 0.025 Then
ActiveSheet.Shapes("Picture 3").Select
Selection.Cut
End If
}
As you see:
Q4 corresponds to Picture 1
Q5 corresponds to Picture 2
Q6 corresponds to Picture 3
...
Q50 corresponds to Picture 47
...
Q103 corresponds to Picture 100
I am at a loss for writing a more elegant code rather than repeating this for 50-100 times! Could you please show me how to do it?
Thanks!


Reply With Quote

It was very, very helpful and I could have never thought of this code by myself since I am struggling to learn VB coding as I go with my task requirements (that's why my knowledge of VB is very shallow
). I put it in my spreadsheet and tested it over many hours and it runs great! 