jrichmeier1
New Member
- Joined
- May 4, 2018
- Messages
- 1
Hello,
I'm coming here for help on a UDF to count unique values with (optional) multiple criteria. I've pieced together the following code but am having trouble getting it to work (it throws a #VALUE error each time).
Is there a way to get this code to work?
<svg class="SnapLinksHighlighter" xmlns="http://www.w3.org/2000/svg"> <rect width="0" height="0"></rect> <!-- Used for easily cloning the properly namespaced rect --> </svg>
I'm coming here for help on a UDF to count unique values with (optional) multiple criteria. I've pieced together the following code but am having trouble getting it to work (it throws a #VALUE error each time).
Is there a way to get this code to work?
Code:
Function CountUniques(CountItems As Range, Optional LookupRange_1 As Range, Optional Criteria_1 As Variant, Optional LookupRange_2 As Range, Optional Criteria_2 As Variant) As Long
'Example: =CountUniques(A:A,B:B,1,C:C,2)
Dim Record As Variant
Dim Results As Long
Results = 0
For Each Record In CountItems
If Cells(Record.Row, Record.Column) <> Cells(Record.Row - 1, Record.Column) Then
If Cells(Record.Row, LookupRange_1.Column) = Criteria_1 Then
If Cells(Record.Row, LookupRange_2.Column) = Criteria_2 Then
Results = Results + 1
End If
End If
End If
Next Record
CountUniques = Results
End Function
<svg class="SnapLinksHighlighter" xmlns="http://www.w3.org/2000/svg"> <rect width="0" height="0"></rect> <!-- Used for easily cloning the properly namespaced rect --> </svg>