Solved

Counter number of red cells

Posted on 2000-03-26
7
313 Views
Last Modified: 2008-10-12
I need to be able to count the number of red(font) cells in a certain row in Excel97...Is this possible in a formula, or is some hefty VBA needed?

Thanks!
0
Comment
Question by:rockies1
  • 3
  • 2
  • 2
7 Comments
 
LVL 17

Expert Comment

by:calacuccia
ID: 2659010
Hi Rockies1,

You will need VBA to do what you ask for. Here's a little routine which will do what you want. I've made it with an inputbox, in which you have to enter the rownumber to check and the result is displayed in a message box.

Sub CountRedCells()
Dim Count As Integer, RowNumber As Integer
RowNumber = InputBox("Enter the row number to check")
For Each c In ActiveSheet.Rows(RowNumber).Cells
If c.Font.ColorIndex = 3 Then Count = Count + 1
Next c
MsgBox ("You have " & Count & " cells with Red text in row " & RowNumber & " !")
End Sub

If you need to count the number of cells with background set to red, the line If... Then... must be replaced by this line:

If c.Interior.ColorIndex = 3 Then Count = Count + 1

Hope this helps

Calacuccia
0
 
LVL 9

Expert Comment

by:antrat
ID: 2659888
Hi rockies 1

If you prefer to have a worksheet formula to do this you can use this a custom function like below:

 Dim Mycell As Range, Mycount As Integer
Function RedFCount(TheRange As Range)
For Each Mycell In TheRange
  If Mycell.Font.ColorIndex = 3 Then
     Mycount = Mycount + 1
  End If
Next
    RedCount = Mycount
 End Function



To put it in hit Alt+F11 then go to Insert>Module and paste it in, then click the top right X and save.

Now either type =RedFCount(A1:A100) or select the function from the User Defined section of the Function Wizard.

TIP: to make the function available in any Workbook:

1: Tools>Macro>Record New Macro

2: Type RedFCount in the "Macro Name" box and select "Personal Macro Workbook"
from the "Store Macro in:" list and click OK.

3:Click any cell then Tools>Macro>Stop Recording.

4:Window>Unhide>Personal.xls

5:Alt+F11 then Ctrl+R and then open "Modules" under "VBAProject (Personal)" from the Project Explorer, (Window with heading "Project-VBAProject"). Then double click "Module1" and paste in the code over the top of what's there.

6:Hit the top right X and then Window>Hide and save.


Hope that helps

antrat

0
 

Author Comment

by:rockies1
ID: 2660924
The problem with those pieces of code is that the red is from conditional formatting, and the code only sees the original formatting...

Any ideas on how to see the conditional formatting?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Accepted Solution

by:
calacuccia earned 100 total points
ID: 2662350
Conditional formatting...

This one is getting hard.
Following code (code originally from VBoukhar, slightly modified by me) generates one user-defined function in your spreadsheet, which can be called from the Excel sheet, by using the function wizard and looking for 'GetColored' in the User-Defined category, or simply by using the worksheet formula
=GetColored(Range) where range can be typed as in other formula's or selected with the mouse as in other formula's. The function will count all cells which match the 'Format Condition' so it will not be perfect for you if you have multiple types of conditional formats, of which you don't want to count some. Also, it gives troubles, when your condition are using string comparison...

If that's the case (any of two mentioned possible cuases, please report back and we'll see what can be done)

Example:
=GetColored(A1:B20) will look for all conditionally formatted & matching cells in your range A1:B20


Function GetColored(a As Range) As Integer
Application.Volatile
  Dim c As Range
  Dim i As Integer
  i = 0
  For Each c In a
    If IsCond(c) Then
      i = i + 1
    End If
  Next c
  GetColored = i
End Function

Function IsCond(a As Range) As Boolean
  IsCond = False
  If a.FormatConditions.Count = 0 Then
    Exit Function
  End If
  Select Case a.FormatConditions(1).Operator
    Case xlEqual:
           If a.Value = a.FormatConditions(1).Formula1 Then
           IsCond = True
           Exit Function
         End If
    Case xlGreater:
         If a.Value > a.FormatConditions(1).Formula1 Then
           IsCond = True
           Exit Function
         End If
    Case xlGreaterEqual:
         If a.Value >= a.FormatConditions(1).Formula1 Then
           IsCond = True
           Exit Function
         End If
    Case xlLess:
         If a.Value < a.FormatConditions(1).Formula1 Then
           IsCond = True
           Exit Function
         End If
    Case xlLessEqual:
         If a.Value <= a.FormatConditions(1).Formula1 Then
           IsCond = True
           Exit Function
         End If
    Case xlNotEqual:
         If a.Value <> a.FormatConditions(1).Formula1 Then
           IsCond = True
           Exit Function
         End If
    Case xlBetween:
         If a.Value >= a.FormatConditions(1).Formula1 And a.Value <= a.FormatConditions(1).Formula2 Then
           IsCond = True
           Exit Function
         End If
    Case xlNotBetween:
         If a.Value < a.FormatConditions(1).Formula1 Or a.Value > a.FormatConditions(1).Formula2 Then
           IsCond = True
           Exit Function
         End If
    End Select
End Function


Hope this helps

Calacuccia
0
 

Author Comment

by:rockies1
ID: 2664178
Ugh...Still no luck...

Check out the actual spreadsheet in question <a href="http://handy.gvi.net/~rockies1/excel.zip">here</a>.

The function goes to the case xlEqual, but the IF never evaluates true...

I'm not real familiar with the functions you've used, so I really can't figure it out...
0
 

Author Comment

by:rockies1
ID: 2664180
Adjusted points from 50 to 100
0
 
LVL 9

Expert Comment

by:antrat
ID: 2664322
This is a tough one. I can only manage to count cells that have red font applied if the condition is true, but it counts the ones that dont't meet the condition as well.

antrat
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stuck in average-1 12 65
Getting rid of #VALUE! 7 19
Adding Additional Criteria to a formula 17 44
extract a substring between backslashes from paths 10 19
Some time ago I was asked to create a VBA function that would calculate a check digit for an input number, using the following procedure: First, sum up all the individual digits in the number If that sum value has more than one digit, then sum up …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question