Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Changing Background Color as a True/False Response | Excel Discussion (Misc queries) | |||
how can I conditionally change font color, or background color? | Excel Worksheet Functions | |||
Change font and background color of several cells based on result of a formula | Excel Discussion (Misc queries) | |||
Default Border, Font Color, and Cell Background Color | Excel Discussion (Misc queries) | |||
Excel 2003 Font Color and Background Color | Excel Discussion (Misc queries) |