Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 24
Default Count by Colour (Text) with other criteria

I am using excel 2000

I have used the following Formula to count the number of times that ABC occurs in red text within the listed range, and it works fine

=PERSONAL.XLS!CountByColorText($G$4:$R$211,3,TRUE, "ABC")

I now want to count the number of times that (F) occurs in red text within the listed range, the difference being that (F) is always only the last three digits in the cell eg ABC (F)

I have tried these formulae

=PERSONAL.XLS!CountByColorText($G$4:$R$211,3,TRUE, "*(F)*")

{=PERSONAL.XLS!CountByColorText(RIGHT($G$4:$R$211, 3),3,TRUE, "(F)")}

=SUMPRODUCT(--(NOT(ISERROR(SEARCH("F",$G$4:$R$211))*(PERSONAL.XL S!CountByColorText($G$4:$R$211,3,TRUE)))))

None of which gave the correct answer

My UDF code is as follows

Code:
Function CountByColorText(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False, _
Optional Str As String = "") As Long

Dim Rng As Range
Dim CheckStr As Boolean
Application.Volatile True

For Each Rng In InRange.Cells
CheckStr = False
If Str = "" _
Or LCase(Rng.Value) = LCase(Str) Then
CheckStr = True
End If

If CheckStr = True Then
If OfText = True Then
CountByColorText = CountByColorText - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColorText = CountByColorText - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
End If
Next Rng

End Function__________________



Any help with a formula that will count (F) when it appears as part of the contents of a cell and is red text much appreciated

In the Example attached, I want the result in cell d14 to be 1
Attached Files
File Type: zip count (F).zip (1.9 KB, 86 views)
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Count by Colour (Text) with other criteria

I think you're going to have to modify your function to support as much as you
want.

For instance, you could use an asterisk for a wildcard "*(F)" to represent ends
with "(F)". You could use "*(F)*" for contains "(F)".

If you're industrious, you could use use the same "escape sequences" that excel
uses:
~* represents an asterisk (not tilde, then wild card)
~? represents a question mark (not tilde, then a single character wild card)
~~ represents a single tilde (no escape sequence)

And if you're really industrious, you may want to learn about regular
expressions.

But if you're only semi-industrious:

Option Explicit
Function CountByColorText(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False, _
Optional Str As String = "") As Long

Dim Rng As Range
Dim CheckStr As Boolean
Dim HasWildCards As Boolean
Application.Volatile True

HasWildCards = CBool(InStr(1, Str, "*", vbTextCompare) 0)

For Each Rng In InRange.Cells
CheckStr = False
If Str = "" Then
CheckStr = True
End If
If HasWildCards Then
If LCase(Rng.Value) Like LCase(Str) Then
CheckStr = True
End If
Else
If LCase(Rng.Value) = LCase(Str) Then
CheckStr = True
End If
End If

If CheckStr = True Then
If OfText = True Then
CountByColorText = CountByColorText - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColorText = CountByColorText - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
End If
Next Rng

End Function

======
Google in the *excel* newsgroups for regular expressions and you'll find lots of
hits.

Paul S wrote:

I am using excel 2000

I have used the following Formula to count the number of times that ABC
occurs in red text within the listed range, and it works fine

=PERSONAL.XLS!CountByColorText($G$4:$R$211,3,TRUE, "ABC")

I now want to count the number of times that (F) occurs in red text
within the listed range, the difference being that (F) is always only
the last three digits in the cell eg ABC (F)

I have tried these formulae

=PERSONAL.XLS!CountByColorText($G$4:$R$211,3,TRUE, "*(F)*")

{=PERSONAL.XLS!CountByColorText(RIGHT($G$4:$R$211, 3),3,TRUE, "(F)")}

=SUMPRODUCT(--(NOT(ISERROR(SEARCH("F",$G$4:$R$211))*(PERSONAL.XL S!CountByColorText($G$4:$R$211,3,TRUE)))))

None of which gave the correct answer

My UDF code is as follows

Code:
Function CountByColorText(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False, _
Optional Str As String = "") As Long

Dim Rng As Range
Dim CheckStr As Boolean
Application.Volatile True

For Each Rng In InRange.Cells
CheckStr = False
If Str = "" _
Or LCase(Rng.Value) = LCase(Str) Then
CheckStr = True
End If

If CheckStr = True Then
If OfText = True Then
CountByColorText = CountByColorText - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColorText = CountByColorText - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
End If
Next Rng

End Function__________________

Any help with a formula that will count (F) when it appears as part of
the contents of a cell and is red text much appreciated

In the Example attached, I want the result in cell d14 to be 1

+-------------------------------------------------------------------+
|Filename: count (F).zip |
|Download: http://www.excelbanter.com/attachment.php?attachmentid=98|
+-------------------------------------------------------------------+

--
Paul S


--

Dave Peterson
  #3   Report Post  
Junior Member
 
Posts: 24
Default

Thx Dave, as I'm a novice in VBA and up against a deadline I took your semi-industrious option, which worked fine

Paul

Quote:
Originally Posted by Dave Peterson View Post
I think you're going to have to modify your function to support as much as you
want.

For instance, you could use an asterisk for a wildcard "*(F)" to represent ends
with "(F)". You could use "*(F)*" for contains "(F)".

If you're industrious, you could use use the same "escape sequences" that excel
uses:
~* represents an asterisk (not tilde, then wild card)
~? represents a question mark (not tilde, then a single character wild card)
~~ represents a single tilde (no escape sequence)

And if you're really industrious, you may want to learn about regular
expressions.

But if you're only semi-industrious:

Option Explicit
Function CountByColorText(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False, _
Optional Str As String = "") As Long

Dim Rng As Range
Dim CheckStr As Boolean
Dim HasWildCards As Boolean
Application.Volatile True

HasWildCards = CBool(InStr(1, Str, "*", vbTextCompare) 0)

For Each Rng In InRange.Cells
CheckStr = False
If Str = "" Then
CheckStr = True
End If
If HasWildCards Then
If LCase(Rng.Value) Like LCase(Str) Then
CheckStr = True
End If
Else
If LCase(Rng.Value) = LCase(Str) Then
CheckStr = True
End If
End If

If CheckStr = True Then
If OfText = True Then
CountByColorText = CountByColorText - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColorText = CountByColorText - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
End If
Next Rng

End Function

======
Google in the *excel* newsgroups for regular expressions and you'll find lots of
hits.

Paul S wrote:

I am using excel 2000

I have used the following Formula to count the number of times that ABC
occurs in red text within the listed range, and it works fine

=PERSONAL.XLS!CountByColorText($G$4:$R$211,3,TRUE, "ABC")

I now want to count the number of times that (F) occurs in red text
within the listed range, the difference being that (F) is always only
the last three digits in the cell eg ABC (F)

I have tried these formulae

=PERSONAL.XLS!CountByColorText($G$4:$R$211,3,TRUE, "*(F)*")

{=PERSONAL.XLS!CountByColorText(RIGHT($G$4:$R$211, 3),3,TRUE, "(F)")}

=SUMPRODUCT(--(NOT(ISERROR(SEARCH("F",$G$4:$R$211))*(PERSONAL.XL S!CountByColorText($G$4:$R$211,3,TRUE)))))

None of which gave the correct answer

My UDF code is as follows

Code:
Function CountByColorText(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False, _
Optional Str As String = "") As Long

Dim Rng As Range
Dim CheckStr As Boolean
Application.Volatile True

For Each Rng In InRange.Cells
CheckStr = False
If Str = "" _
Or LCase(Rng.Value) = LCase(Str) Then
CheckStr = True
End If

If CheckStr = True Then
If OfText = True Then
CountByColorText = CountByColorText - _
(Rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColorText = CountByColorText - _
(Rng.Interior.ColorIndex = WhatColorIndex)
End If
End If
Next Rng

End Function__________________

Any help with a formula that will count (F) when it appears as part of
the contents of a cell and is red text much appreciated

In the Example attached, I want the result in cell d14 to be 1

+-------------------------------------------------------------------+
|Filename: count (F).zip |
|Download: http://www.excelbanter.com/attachment.php?attachmentid=98|
+-------------------------------------------------------------------+

--
Paul S


--

Dave Peterson
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
Count text cells based on two criteria aet999 Excel Worksheet Functions 9 May 2nd 09 03:20 AM
count cells,containing text, based on more than one criteria? Sarfraz Excel Worksheet Functions 1 March 30th 07 11:20 PM
Count items when specific text and date criteria are met javamom Excel Worksheet Functions 8 April 24th 06 09:28 PM
How do I count wildcard text meeting certain criteria in EXCEL? cybermaksim Excel Worksheet Functions 1 February 17th 06 03:03 AM
Count by Colour (Text) with other criteria Paul Sheppard Excel Discussion (Misc queries) 1 January 18th 06 03:41 PM


All times are GMT +1. The time now is 02:28 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"