Count for Group of Shape with different color

Wiz Lee

New Member
Joined
Nov 8, 2017
Messages
18
Greetings,


I have 4 shapes in one group in Excel Workbook, I want is when I color 2 shapes in yellow, Cells(6, 21) will appear 1 and cells(6, 22) will appear 2, and the other 2 shapes I color in blue, cells(9, 21) still remain 0 and Cells(9, 22) are 2.

Below is my vba code I apply in my workbook but did not work as I want.

Code:
Private Sub Worksheet_Activate()    Dim shp As Shape
    Dim shprange As ShapeRange
    Dim CountyellowShape As Long
    Dim CountorangeShape As Long
    Dim CountpinkShape As Long
    Dim CountblueShape As Long


    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, 255, 0) Then CountChildShapeYELLOW = CountChildShapeYELLOW + 1
            If shprange.Fill.ForeColor.RGB = RGB(255, 153, 0) Then CountChildShapeORANGE = CountChildShapeORANGE + 1
            If shprange.Fill.ForeColor.RGB = RGB(255, 102, 153) Then CountChildShapePINK = CountChildShapePINK + 1
            If shprange.Fill.ForeColor.RGB = RGB(0, 176, 240) Then CountChildShapeBLUE = CountChildShapeBLUE + 1
             
        End If
    Next shp
     
    For Each shp In Sheet1.Shapes
        If shp.Fill.ForeColor.RGB = RGB(255, 255, 0) Then CountShapeYELLOW = CountShapeYELLOW + 1
        If shp.Fill.ForeColor.RGB = RGB(255, 153, 0) Then CountShapeORANGE = CountShapeORANGE + 1
        If shp.Fill.ForeColor.RGB = RGB(255, 102, 153) Then CountShapePINK = CountShapePINK + 1
        If shp.Fill.ForeColor.RGB = RGB(0, 176, 240) Then CountShapeBLUE = CountShapeBLUE + 1
    Next shp
     
    Sheet1.Cells(6, 21) = CountShapeYELLOW + CountChildShapeYELLOW
    Sheet1.Cells(7, 21) = CountShapeORANGE + CountChildShapeORANGE
    Sheet1.Cells(8, 21) = CountShapePINK + CountChildShapePINK
    Sheet1.Cells(9, 21) = CountShapeBLUE + CountChildShapeBLUE
    
    
        For Each shp In Sheet1.Shapes
        If shp.Type = msoGroup Then
            Set shprange = shp.Ungroup
            For Each grpShp In shprange
            
              If grpShp.Fill.ForeColor.RGB = RGB(255, 255, 0) Then CountChildShapeYELLOW = CountChildShapeYELLOW + 1
              If grpShp.Fill.ForeColor.RGB = RGB(255, 153, 0) Then CountChildShapeORANGE = CountChildShapeORANGE + 1
              If grpShp.Fill.ForeColor.RGB = RGB(255, 102, 153) Then CountChildShapePINK = CountChildShapePINK + 1
              If grpShp.Fill.ForeColor.RGB = RGB(0, 176, 240) Then CountChildShapeBLUE = CountChildShapeBLUE + 1
            Next grpShp
            shprange.Group
        Else
          If shp.Fill.ForeColor.RGB = RGB(255, 255, 0) Then CountShapeYELLOW = CountShapeYELLOW + 1
          If shp.Fill.ForeColor.RGB = RGB(255, 153, 0) Then CountShapeORANGE = CountShapeORANGE + 1
          If shp.Fill.ForeColor.RGB = RGB(255, 102, 153) Then CountShapePINK = CountShapePINK + 1
          If shp.Fill.ForeColor.RGB = RGB(0, 176, 240) Then CountShapeBLUE = CountShapeBLUE + 1
        End If
    Next shp
     
    Sheet1.Cells(6, 22) = CountShapeYELLOW + CountChildShapeYELLOW
    Sheet1.Cells(7, 22) = CountShapeORANGE + CountChildShapeORANGE
    Sheet1.Cells(8, 22) = CountShapePINK + CountChildShapePINK
    Sheet1.Cells(9, 22) = CountShapeBLUE + CountChildShapeBLUE
    
    End Sub

Thanking in advance.
Wiz Lee
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
When I run the code, If I put the different color in group shift, Cells(6, 21) will change to 0. I need the Cells(6, 21) remain as 1.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,020
Members
448,939
Latest member
Leon Leenders

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