Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count shapes with same color

Status
Not open for further replies.

Tze Chyi

Technical User
Aug 25, 2016
11
MY
hi,
How could I count the shapes with same color?
Dim shp As Shape
Dim vbYellow As Long
Dim vbGrey As Long
Dim vbRed As Long
Dim vbGreen As Long

If ActiveSheet.Shapes.ForeColor.RGB = vbYellow Then
Sheet1.Cells(2, 1) = ActiveSheet.Shapes.Count
End If
 
Hi,

Just to clarify, if you had two red, three yellow, one green and one blue, the result would be five, correct?

BTW, vbRed, vbYellow, vbGreen etc, are VBA constants, so you do not want to declare any new variables.
As you can see, each color constant has a specific value (no gray constant) Depending on how each shape's color was assigned, the color constant may not have the exact same value that the shape has.

Bottom line, you need to refine your stated requirement.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Hi SkipVought, Mintjulep
I had assigned another macro to insert the shapes with fixed color (vbRed, vbGreen, etc)
Then I'm stuck to get the number of shapes count.
What I need is, lets say I have 3 green shapes, 2 red shapes, 2 yellow shapes, I want the result to be show as
Cell (1,1): 3 (for green)
Cell (1,2): 2 (for red)
Cell (1,3): 2 (for yellow)
and so on
 
You need a loop [tt]For Each xShape in ActiveSheet.Shapes[/tt], do three colour tests for xShape ForeColor, increase proper temporary variables or target cells if any test passed, and finally fill target cells with results (if working immediately with target cells, clear theif contents before initialising the loop).

combo
 
>I had assigned another macro to insert the shapes with fixed color

Surely you therefore know the number of shapes with the same colour, then?
 
Please post the code you used to assign these color properties.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Hi,
Below is the code for color selection.
[highlight #204A87]Private Sub Green_Click()
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 70, 206, _
172, 91).Select
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.RGB = vbGreen
.BackColor.RGB = vbGreen
.ForeColor.Brightness = 0.8
.Solid
End With
With Selection.ShapeRange.Line
.Visible = msoTrue
.ForeColor.RGB = vbGreen
.ForeColor.Brightness = 0.8
.Transparency = 0
End With

With SelectShapeColor
Unload Me
End With

End Sub

The number of shapes of each colors may up to 100+ in the end, I cant really remember how much of each color I had added.
Thus, I need a summary of the total number of shapes of each color added in the excel sheet.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top