VBA UDF: Count Unique Values with Optional Multiple Criteria

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?

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>
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
change
Code:
[COLOR=#333333]Dim Record As Variant[/COLOR]
to
Code:
Dim Record As Range
 
Last edited:
Upvote 0
With your sample formula, the range object Record is assigned cell A1 during the first iteration of the For Each/Next loop. As a result, Record.Row - 1 returns 0. And so Cells(Record.Row - 1, Record.Column) causes an error. Before entering the For Each/Next loop, try re-defining CountItems as follows...

Code:
With CountItems
    Set CountItems = Application.Intersect(.Worksheet.UsedRange, .Resize(.Count - 1).Offset(1, 0))

End With


This will offset the range one row down from its original location, and only for the used range so that you don't have to deal with the whole column.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top