SOLVED

Checking for any shapes selected ??

Brass Contributor

I'm trying to find a way to check if any Shapes are selected because executing then next code is meaningless if no selection is done.

There is LOTS of samples out there but way too sophisticated - I wonder WHY it is so complicated to do such simple thing !

I found this code inserted in the start of any of my Subs that need to have some shapes selected:

 

If TypeName(Selection) = "Range" Then Exit Sub

 

It works eventhough NOTHING seems to be selected - maybe there IS something selected, but I have turned "Marking Objects" ON and therefore can't see the active cell marking.

 

But have I just misunderstood - is there a better practice when SHAPES must be selected ?

2 Replies
best response confirmed by keldsor (Brass Contributor)
Solution

@keldsor This seems to work:

Sub TestIfShapeSelected()
    Dim shp As Shape
    Dim obj As Object
    On Error Resume Next
    Set obj = Selection.ShapeRange(1)
    If obj Is Nothing Then
        MsgBox "No shape selected"
    Else
        For Each shp In ActiveSheet.Shapes
            If shp Is obj Then
                'Shape selected
                MsgBox "You have selected shape '" & shp.Name & "'"
            End If
        Next
    End If
End Sub
THX ... yesh, it could be stripped a little to minimize the code ;)
1 best response

Accepted Solutions
best response confirmed by keldsor (Brass Contributor)
Solution

@keldsor This seems to work:

Sub TestIfShapeSelected()
    Dim shp As Shape
    Dim obj As Object
    On Error Resume Next
    Set obj = Selection.ShapeRange(1)
    If obj Is Nothing Then
        MsgBox "No shape selected"
    Else
        For Each shp In ActiveSheet.Shapes
            If shp Is obj Then
                'Shape selected
                MsgBox "You have selected shape '" & shp.Name & "'"
            End If
        Next
    End If
End Sub

View solution in original post