Forum Discussion
keldsor
Oct 27, 2022Brass Contributor
Checking for any shapes selected ??
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 ?
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
2 Replies
- JKPieterseSilver Contributor
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
- keldsorBrass ContributorTHX ... yesh, it could be stripped a little to minimize the code 😉