I am trying to create a custom function for use in Excel 2003 (Excel 2007 has the function I need but I have to use 2003 at work). I want the function to count based on 3 criteria but I can not get it to count using only 1 criteria. Here is my code:
Function sumviolation(strViol As String) As Integer
Dim intCount As Integer
Dim rCel, rViol As Range
rViol = Range("Data!I2: I1004")
intCount = 0
For Each rCel In rViol
If rCel.Value = strViol Then intCount = intCount + 1
Next rCel
sumviolation = intCount
End Function
The cell where I want the value contains:
=sumviolation("SPEEDING")
Excel shows #VALUE! in the cell. I have tried formatting the cell as General, Number, and Text -- formatted as Text is simply displays the formula. I have tried changing the function return value to variant with the same result.
I realize there is a built in formula that will work but I want to expand this function to check 2 additional values in different ranges, once it is working.
What am I missing?
Function sumviolation(strViol As String) As Integer
Dim intCount As Integer
Dim rCel, rViol As Range
rViol = Range("Data!I2: I1004")
intCount = 0
For Each rCel In rViol
If rCel.Value = strViol Then intCount = intCount + 1
Next rCel
sumviolation = intCount
End Function
The cell where I want the value contains:
=sumviolation("SPEEDING")
Excel shows #VALUE! in the cell. I have tried formatting the cell as General, Number, and Text -- formatted as Text is simply displays the formula. I have tried changing the function return value to variant with the same result.
I realize there is a built in formula that will work but I want to expand this function to check 2 additional values in different ranges, once it is working.
What am I missing?