PDA

View Full Version : [SOLVED] How to count no of shapes in a group?



ooitzechyi
09-14-2016, 09:13 PM
hi,
How could I count no of specify shape in a group? (eg: I have 2 red color shapes in a group)
Tried with

Dim sh As Sheet1
Dim shp As Shape
Dim CountChildShapeRed As Long

For Each shp In Sheet1.Shapes

Select Case Shapes
Case Is = msoGroup
For x = 1 To Shapes.GroupItems.Count

If Sheet1.Shapes(x).HasChildShape Then
If Sheet1.Shapes.Child.Fill.ForeColor.RGB = RGB(255, 204, 255) Then CountChildShapeRed = CountChildShapeRed + 1
End If
Next
End Select
Next shp
Sheet1.Cells(4, 1) = CountChildShapeRed

The result I get is blank.

mana
09-15-2016, 01:48 AM
child only?


Option Explicit


Sub test()
Dim shp As Shape
Dim x As Long
Dim CountChildShapeRed As Long

For Each shp In Sheet1.Shapes

If shp.Type = msoGroup Then
For x = 1 To shp.GroupItems.Count
If shp.GroupItems(x).Fill.ForeColor.RGB = vbRed Then
CountChildShapeRed = CountChildShapeRed + 1
End If
Next
Else
' If shp.Fill.ForeColor.RGB = vbRed Then
' CountChildShapeRed = CountChildShapeRed + 1
' End If
End If
Next

Sheet1.Cells(4, 1).Value = CountChildShapeRed

End Sub

ooitzechyi
09-15-2016, 02:03 AM
Hi,
Thanks for your help. It did help me in solving this problem but just there will be one error prompted as Application-defined or Object-defined error (Error 1004).
I tried to modified it into as below and it works!


Private Sub Ignore_Click()
Dim sh As Sheet1
Dim shp As Shape
Dim shprange As ShapeRange
Dim oMyGroup As Shape
Dim CountRedShape As Long

Sheet1.Cells(2, 1).Resize(4).ClearContents
Sheet1.Shapes.SelectAll

For Each shp In Sheet1.Shapes

If shp.Type = msoGroup Then
Set shprange = shp.Ungroup
Set oMyGroup = shprange.Group

If shprange.Fill.ForeColor.RGB = RGB(255, 204, 255) Then CountChildShapeRed = CountChildShapeRed + 1

End If
Next shp

For Each shp In Sheet1.Shapes
If shp.Fill.ForeColor.RGB = RGB(255, 204, 255) Then CountShapeRED = CountShapeRED + 1
Next shp

Sheet1.Cells(4, 1) = CountShapeRED + CountChildShapeRed