![]() |
Test for an empty selection
My macro creates new shapes on the worksheet.
I want to group them but if no shapes were created the macro fails. How do I test for valid selection. I have tied: If Not Selection Is Nothing Then Selection.ShapeRange.Group If Not Selection.ShapeRange Is Nothing Then Selection.... But nothing works. It says "Object doesn't support this property or method" Thanks *** Sent via Developersdex http://www.developersdex.com *** |
Test for an empty selection
Maybe you could use a shape object to test if at least one shape exists
on the sheet eg: Dim shp As Shape On Error Resume Next Set shp = ActiveSheet.Shapes(1) On Error GoTo 0 If Not shp Is Nothing Then MsgBox "Do this" Else MsgBox "exit" End If Hope this helps Rowan Edward Ulle wrote: My macro creates new shapes on the worksheet. I want to group them but if no shapes were created the macro fails. How do I test for valid selection. I have tied: If Not Selection Is Nothing Then Selection.ShapeRange.Group If Not Selection.ShapeRange Is Nothing Then Selection.... But nothing works. It says "Object doesn't support this property or method" Thanks *** Sent via Developersdex http://www.developersdex.com *** |
Test for an empty selection
Hi Edward,
Problems will also arise if there are less than 2 shapes or less than 2 existing groups of shapes. Try something like: '==================== Sub TestIt() Dim sr As ShapeRange With ActiveSheet.Shapes .SelectAll Selection.Ungroup If .Count = 2 Then .SelectAll Set sr = Selection.ShapeRange sr.Group ' Else 'Less than 2 shapes exist, do something, e.g.: MsgBox "The number of shapes = " & .Count End If End With If Not sr Is Nothing Then 'do something, e.g.: With sr.Fill .ForeColor.SchemeColor = 13 .Visible = msoTrue .Solid End With End If End Sub '==================== --- Regards, Norman "Edward Ulle" wrote in message ... My macro creates new shapes on the worksheet. I want to group them but if no shapes were created the macro fails. How do I test for valid selection. I have tied: If Not Selection Is Nothing Then Selection.ShapeRange.Group If Not Selection.ShapeRange Is Nothing Then Selection.... But nothing works. It says "Object doesn't support this property or method" Thanks *** Sent via Developersdex http://www.developersdex.com *** |
Test for an empty selection
Rowan,
I was trying to avoid error handling. I thought a simple property check would do it. See my response to Norman. Thanks. Ed *** Sent via Developersdex http://www.developersdex.com *** |
Test for an empty selection
Norman,
The situation is a little more complicated than that. I build up layers of grouped shapes on each worksheet. Some times there are not shapes in that layer and I thought a property check would work. Also I didn't want to use error handling as a programmer should anticipate something as simple as this and handle it. What I have decided to do is simple increment a counter when a new shape is created in that layer. If the counter is zero than don't try to group them into that layer. Thank. Ed *** Sent via Developersdex http://www.developersdex.com *** |
All times are GMT +1. The time now is 02:29 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com