View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.misc
Kevin B
 
Posts: n/a
Default cell formula for counting instances of text?

This UDF will do what you want and return a string that looks like the
following:

Pass=#/Fail = #/ NA = #

Where # = the number of occurrences for that value.

Press Alt+F11 to open the VBE, insert a module and paste the following
function in.

Use it like you would any other function.

=PASSFAILNA(A1:A10)

The line Application.Volatile will force the formula to recalc with each
recalculation of the spreadsheet.

Function PassFailNA(rng As Range) As String

Dim intPass As Integer
Dim intFail As Integer
Dim intNA As Integer
Dim r As Range
Dim lngCells As Long
Dim varVal As Variant

Set r = rng
Application.Volatile

For lngCells = 1 To r.Cells.Count
varVal = r.Cells(lngCells)
Select Case varVal
Case Is = "Pass"
intPass = intPass + 1
Case Is = "Fail"
intFail = intFail + 1
Case Is = "N/A"
intNA = intNA + 1
Case Else
End Select
Next lngCells

PassFailNA = "Pass = " & intPass & "/Fail = " & _
intFail & "/NA = " & intNA

Set r = Nothing
Exit Function

End Function

--
Kevin Backmann


"Samuel" wrote:

I have a cell that could have PASS, FAIL, or N/A
What kind of formula can I use to count the number of times PASS FAIL
or N/A occurs in the column and write the count to a header cell?
I can do math to sum, avg, etc, but I am not sure how to deal with
text.
Thank you,
Sam