INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Count shapes with same color

Count shapes with same color

(OP)
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

RE: Count shapes with same color

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. https://msdn.microsoft.com/en-us/library/aa265064(...

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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Count shapes with same color

How did you decide what color to make them?

RE: Count shapes with same color

(OP)
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

RE: Count shapes with same color

You need a loop For Each xShape in ActiveSheet.Shapes, 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

RE: Count shapes with same color

>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?

RE: Count shapes with same color

Please post the code you used to assign these color properties.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Count shapes with same color

(OP)
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

RE: Count shapes with same color

Count as you color.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close