Help with an if error on excel vba

BORUCH

Active Member
Joined
Mar 1, 2016
Messages
301
hi

i have this piece of code in my excel macro.

Code:
ActiveSheet.Shapes.Range(Array("Picture 2")).Select

The problem i'm having is that sometimes depending on the sheet that i'm running the code on it can be "image1.png etc..

how can i modify this line that if it errors out it will select image1.png or image2.png

thanks
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
if we're talking 2-3 options then this should do it:
Code:
...
On Error Resume Next
    Err.Clear
    ActiveSheet.Shapes.Range(Array("Picture 1")).Select
    If Err > 0 Then
        Err.Clear
        ActiveSheet.Shapes.Range(Array("Picture 2")).Select
        If Err > 0 Then
            Err.Clear
            ActiveSheet.Shapes.Range(Array("Picture 3")).Select
        End If
    End If
On Error GoTo 0
...
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
With this you select the first image that exists on the sheet, no matter what its name.

Code:
    For Each shp In ActiveSheet.Shapes
        shp.Select
        Exit For
    Next
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
Or if you really want to select all shapes:
Code:
ActiveSheet.Shapes.Selectall
 

Watch MrExcel Video

Forum statistics

Threads
1,109,446
Messages
5,528,802
Members
409,836
Latest member
karnasrinivas
Top