Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 28
Default count if font and background color condition is true

Hi,
I want to count the number of cells that have the font color in -4105
(autocolor) and a background color of -4142 (no fill). I can't figure out
how to make the code work to count when both conditions are met.

I have found the following code:

Function CountByColor(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Long
'
' This function return the number of cells in InRange with
' a background color, or if OfText is True a font color,
' equal to WhatColorIndex.
'
Dim Rng As Range
Application.Volatile True

For Each Rng In InRange.Cells
If OfText = True Then
CountByColor = CountByColor - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColor = CountByColor - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
Next Rng

End Function


  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 28
Default count if font and background color condition is true

I should also mention that I need my spreadsheet to display all three counts,

1. how many font in autocolor (which is the If statement)
2. how many background in no fill (which is the Else statement)
3. how many have both autocolor and no fill (which what I need and can't
figure out)

Thanks,

"Ivano" wrote:

Hi,
I want to count the number of cells that have the font color in -4105
(autocolor) and a background color of -4142 (no fill). I can't figure out
how to make the code work to count when both conditions are met.

I have found the following code:

Function CountByColor(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Long
'
' This function return the number of cells in InRange with
' a background color, or if OfText is True a font color,
' equal to WhatColorIndex.
'
Dim Rng As Range
Application.Volatile True

For Each Rng In InRange.Cells
If OfText = True Then
CountByColor = CountByColor - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColor = CountByColor - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
Next Rng

End Function


  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1,069
Default count if font and background color condition is true

You'll need a different function to return the count of cells meeting both
conditions.

Public Function CountByColor2(InRange As Range, _
FontColorIndex As Integer, _
FillColorIndex As Integer) As Long
Dim Rng As Range
Application.Volatile True
For Each Rng In InRange.Cells
If (Rng.Font.ColorIndex = FontColorIndex) And _
(Rng.Interior.ColorIndex = FillColorIndex) Then
BothCnt = BothCnt + 1
CountByColor2 = CountByColor2 + 1
End If
Next Rng
End Function

To call it:
=CountByColor2(A1:B11,-4105,-4142)

Hope this helps,

Hutch

"Ivano" wrote:

I should also mention that I need my spreadsheet to display all three counts,

1. how many font in autocolor (which is the If statement)
2. how many background in no fill (which is the Else statement)
3. how many have both autocolor and no fill (which what I need and can't
figure out)

Thanks,

"Ivano" wrote:

Hi,
I want to count the number of cells that have the font color in -4105
(autocolor) and a background color of -4142 (no fill). I can't figure out
how to make the code work to count when both conditions are met.

I have found the following code:

Function CountByColor(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Long
'
' This function return the number of cells in InRange with
' a background color, or if OfText is True a font color,
' equal to WhatColorIndex.
'
Dim Rng As Range
Application.Volatile True

For Each Rng In InRange.Cells
If OfText = True Then
CountByColor = CountByColor - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColor = CountByColor - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
Next Rng

End Function


  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 28
Default count if font and background color condition is true

Hi Tom,
Thanks. It works and has helped

"Tom Hutchins" wrote:

You'll need a different function to return the count of cells meeting both
conditions.

Public Function CountByColor2(InRange As Range, _
FontColorIndex As Integer, _
FillColorIndex As Integer) As Long
Dim Rng As Range
Application.Volatile True
For Each Rng In InRange.Cells
If (Rng.Font.ColorIndex = FontColorIndex) And _
(Rng.Interior.ColorIndex = FillColorIndex) Then
BothCnt = BothCnt + 1
CountByColor2 = CountByColor2 + 1
End If
Next Rng
End Function

To call it:
=CountByColor2(A1:B11,-4105,-4142)

Hope this helps,

Hutch

"Ivano" wrote:

I should also mention that I need my spreadsheet to display all three counts,

1. how many font in autocolor (which is the If statement)
2. how many background in no fill (which is the Else statement)
3. how many have both autocolor and no fill (which what I need and can't
figure out)

Thanks,

"Ivano" wrote:

Hi,
I want to count the number of cells that have the font color in -4105
(autocolor) and a background color of -4142 (no fill). I can't figure out
how to make the code work to count when both conditions are met.

I have found the following code:

Function CountByColor(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Long
'
' This function return the number of cells in InRange with
' a background color, or if OfText is True a font color,
' equal to WhatColorIndex.
'
Dim Rng As Range
Application.Volatile True

For Each Rng In InRange.Cells
If OfText = True Then
CountByColor = CountByColor - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColor = CountByColor - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
Next Rng

End Function


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Background Color as a True/False Response ajurczak Excel Discussion (Misc queries) 1 May 10th 07 06:13 PM
how can I conditionally change font color, or background color? MOHA Excel Worksheet Functions 3 August 21st 06 06:57 PM
Change font and background color of several cells based on result of a formula Zenaida Excel Discussion (Misc queries) 2 April 27th 06 06:46 PM
Default Border, Font Color, and Cell Background Color Elijah Excel Discussion (Misc queries) 1 October 28th 05 04:10 PM
Excel 2003 Font Color and Background Color DrankPA6 Excel Discussion (Misc queries) 1 August 12th 05 11:43 PM


All times are GMT +1. The time now is 08:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"