Help with an if error on excel vba

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
548
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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
...
 
Upvote 0
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
 
Upvote 0
Or if you really want to select all shapes:
Code:
ActiveSheet.Shapes.Selectall
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top