0

I am trying to calculate the count of Unique values based on a condition.

For example,

For a value in column B, I am trying to count the Unique values in Column C through VBA.

I know how to do it using Excel formula -

 =SUMPRODUCT((B2:B12<>"")*(A2:A12=32)/COUNTIF(B2:B12,B2:B12))

that value for 32 is dynamic - Programmatically I am calling them inside my vba code as Name

This is my code :

Application.WorksheetFunction.SumProduct((rng <> "") * (rng2 = Name) / CountIfs(rng, rng))

This is the sample data with the requirement

DATA

Alternatively, I Concatenated both the columns for keeping it simple and hoping to identify the Unique values which starts with name* method.

I don't know where I am going wrong. Kindly share your thoughts.

7
  • 1
    When you call a worksheet function from VBA all arguments must be passed comma separated as a ParamArray. What you are trying to do, of course, is passing nested worksheet functions, CountIfs within Sumproduct. I suspect it can actually be done, but it is so complicated that I never found interest in learning how to do it. It would be preferable over comparable VBA functions only when the database is quite large. Commented Apr 10, 2017 at 3:07
  • 1
    That's Application.WorksheetFunction.CountIfs Commented Apr 10, 2017 at 3:11
  • @Mat'sMug Yes but I am still struck in this - How do you identify the count of Unique values in a column which starts with say - John ? Commented Apr 10, 2017 at 3:29
  • 1
    That's a different problem altogether - one that your question does not describe. Please edit your question so that it can be answered appropriately - see minimal reproducible example. Commented Apr 10, 2017 at 3:32
  • Yes @Mat'sMug I edited my question now Sir as an alternative method. I believe that will reduce the complexity of the work but still I am struck with a formula for that. Please help me if you can. Commented Apr 10, 2017 at 3:35

2 Answers 2

1

You may try something like this...

Function GetUniqueCount(Rng1 As Range, Lookup As String) As Long
Dim x, dict
Dim i As Long, cnt As Long
Set dict = CreateObject("Scripting.Dictionary")
x = Rng1.Value
For i = 1 To UBound(x, 1)
    If x(i, 1) = Lookup Then
        dict.Item(x(i, 1) & x(i, 2)) = ""
    End If
Next i
GetUniqueCount = dict.Count
End Function

Then you can use it like below...

=GetUniqueCount($A$2:$B$10,C2)

Where A2:B10 is the data range and C2 is the name criteria.

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Added another screenshot. It returned the correct output.
It worked Sir. My mistake. Sorry, I had a typo in the data.
No problem. :) Glad it worked for you. Please take a moment to accept the solution to mark your question as Solved.
0

I'd put the values into an array, create a temporary 2nd array and only add values to this array if they are not already present, and then replace the original array. Then it's just a simple matter to sum the unique values:

Sub Unique

dim arr(10) as variant, x as variant
dim arr2() as variant

for x = 1 to 10 ' or whatever
   arr(x) = cells(x, 1) ' or whatever
next x

arr2 = UniqueValuesArray(arr)

' now write some code to count the unique values, you get the idea

End Sub

Function UniqueValuesArray(arr As Variant) As Variant()

Dim currentRow, arrpos As Long
Dim uniqueArray() As Variant
Dim x As Long

arrpos = 0
ReDim uniqueArray(arrpos)

For x = 0 To UBound(arr)
    If UBound(Filter(uniqueArray, arr(x))) = -1 Then
        ReDim Preserve uniqueArray(arrpos)
        uniqueArray(arrpos) = arr(x)
        arrpos = arrpos + 1
    End If
Next x

UniqueValuesArray = uniqueArray

End Function

1 Comment

It throws an error saying. Cannot assign to an array ! I am trying to debug this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.