PDA

View Full Version : Count by Word



tqm1
02-29-2008, 03:42 AM
Dear Experts

Column A has following data

59 DB South
40 DB
41 DNB
42 DB
42 DB West
43 DB
44 DNB east
45 DB
46 DB
51 Asia TWo
52 DB

I want to get folloiwng answers

Rows that contains Word "DB"=8
Rows that contains Word "DNB"=2
Rows that contains Word "asia"=1

Please help

Bob Phillips
02-29-2008, 04:27 AM
=COUNTIF(A:A,"*DB*")

etc.

Simon Lloyd
02-29-2008, 04:39 AM
This is crude but....


="DB = " & Countif(A:A,"DB") & " DNB = " & Countif(A:A,"DNB") & " asia = " & countif(A:A,"asia")

matthewspatrick
02-29-2008, 05:56 AM
Depends on what you mean by "contains the word".

For example, let's say that we change "43 DB" to "43 DBX". Bob's formula will still count the DBX when looking for the DB's. Only you can say if this is what you need.

To match on words, you could try fooling around with this:


Option Explicit

Function RegExpCountIf(Pattern As String, MatchCase As Boolean, Rng As Range)
Dim x As Range
Dim Test As String
Dim Counter As Long

For Each x In Rng.Cells
Test = RegExpFind(CStr(x), Pattern, 1, MatchCase)
If Test <> "" Then Counter = Counter + 1
Next

RegExpCountIf = Counter

End Function

Function RegExpFind(LookIn As String, PatternStr As String, Optional Pos, _
Optional MatchCase As Boolean = True)
' This function uses Regular Expressions to parse a string (LookIn), and return matches to a
' pattern (PatternStr). Use Pos to indicate which match you want:
' Pos omitted : function returns a zero-based array of all matches
' Pos = 0 : the last match
' Pos = 1 : the first match
' Pos = 2 : the second match
' Pos = <positive integer> : the Nth match
' If Pos is greater than the number of matches, is negative, or is non-numeric, the function
' returns an empty string. If no match is found, the function returns an empty string

' If MatchCase is omitted or True (default for RegExp) then the Pattern must match case (and
' thus you may have to use [a-zA-Z] instead of just [a-z] or [A-Z]).

' If you use this function in Excel, you can use range references for any of the arguments.
' If you use this in Excel and return the full array, make sure to set up the formula as an
' array formula. If you need the array formula to go down a column, use TRANSPOSE()

Dim RegX As Object
Dim TheMatches As Object
Dim Answer() As String
Dim Counter As Long

' Evaluate Pos. If it is there, it must be numeric and converted to Long
If Not IsMissing(Pos) Then
If Not IsNumeric(Pos) Then
RegExpFind = ""
Exit Function
Else
Pos = CLng(Pos)
End If
End If

' Create instance of RegExp object
Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Pattern = PatternStr
.Global = True
.IgnoreCase = Not MatchCase
End With

' Test to see if there are any matches
If RegX.Test(LookIn) Then

' Run RegExp to get the matches, which are returned as a zero-based collection
Set TheMatches = RegX.Execute(LookIn)

' If Pos is missing, user wants array of all matches. Build it and assign it as the
' function's return value
If IsMissing(Pos) Then
ReDim Answer(0 To TheMatches.Count - 1) As String
For Counter = 0 To UBound(Answer)
Answer(Counter) = TheMatches(Counter)
Next
RegExpFind = Answer

' User wanted the Nth match (or last match, if Pos = 0). Get the Nth value, if possible
Else
Select Case Pos
Case 0 ' Last match
RegExpFind = TheMatches(TheMatches.Count - 1)
Case 1 To TheMatches.Count ' Nth match
RegExpFind = TheMatches(Pos - 1)
Case Else ' Invalid item number
RegExpFind = ""
End Select
End If

' If there are no matches, return empty string
Else
RegExpFind = ""
End If

' Release object variables
Set RegX = Nothing
Set TheMatches = Nothing

End Function


You would use it like this to match words:

=RegExpCountIf("\bDB\b",FALSE,A1:A100)

tqm1
02-29-2008, 06:28 AM
=COUNTIF(A:A,"*DB*")

etc.


The above command work fine, now here is second part of my question

-Column A------ Column B
59 DB South---------1
40 DB---------------2
41 DNB--------------3
42 DB---------------4
42 DB West---------5
43 DB---------------6
44 DNB east---------7
45 DB---------------8
46 DB---------------9
51 Asia TWo---------5
52 DB---------------6

Sum of colum B AGAINST column A that contains Word "DB"=41
Sum of colum B AGAINST column A that contains Word "DNB"=10
Sum of colum B AGAINST column A that contains Word "ASIA"=5

Please help again

Bob Phillips
02-29-2008, 06:32 AM
Lookup up SUMIF in help, it is very similar to COUNTIF.

tqm1
02-29-2008, 06:45 AM
Lookup up SUMIF in help, it is very similar to COUNTIF.

Dear Sir,

I used this command but it reply nothing

=SUMIF(A:A,"DB*",B:B)

Please help again

Bob Phillips
02-29-2008, 07:13 AM
Look again at my COUNTIF and then your SUMIF.