Count Unique based on two criteria VBA

-=NO=-

New Member
Joined
May 9, 2011
Messages
27
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello Dears

Please if anyone can support in that;

I need to convert the below formula to VBA code,

{=SUM(IF(("City"=Receiving[BRANCH])*('Build Dashboard'!I12=Receiving[CATEGORY LEVEL]), 1/COUNTIFS(Receiving[BRANCH],"City",Receiving[RECEIPT NUMBER],Receiving[RECEIPT NUMBER],Receiving[CATEGORY LEVEL],'Build Dashboard'!I12)), 0)}

The issue that I need a code to make a count for unique values depending on two different criteria, the count will be added in a table according to the input criteria

As per table below; the two criteria are (City & Category) and the unique count range is (Receipt no.).

CityCategoryReceipt no.
XXX222110130683
XXX222110130683
YYX2310124850
YXX2110123413
XXX2110123415
YYX2110123419

Then, the data developed will be copied in the below table
CityCategoryCount
XXX??
YYX??
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
If I am understanding your request then with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"City", "Category"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

Book2
ABC
2XXX3
3YYX2
4YXX1
Table3
 
Upvote 0
If I am understanding your request then with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"City", "Category"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

Book2
ABC
2XXX3
3YYX2
4YXX1
Table3
No sir; the first row should be counted as 2 (as there's one receipt of 3 is duplicated)
 
Upvote 0
If this expected results
CityCategoryCount
XXX2
YYX1
YXX1
XXX1
YYX1

Then try
VBA Code:
Sub test()
    Dim a As Variant, i, ii, x
    a = Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 3)
    With CreateObject("scripting.dictionary")
        For i = 2 To UBound(a)
            If a(i, 1) <> "" Then
                If Not .exists(a(i, 3)) Then
                    .Add (a(i, 3)), Array(a(i, 1), a(i, 2), 1)
                Else
                    x = .Item(a(i, 3))
                    x(0) = x(0): x(1) = x(1): x(2) = x(2) + 1
                    .Item(a(i, 3)) = x
                End If
            End If
        Next
        Cells(1, 5) = Cells(1, 1): Cells(1, 6) = Cells(1, 2): Cells(1, 7) = "Count"
        Cells(2, 5).Resize(.Count, 3) = Application.Transpose(Application.Transpose(.Items))
    End With
End Sub
 
Upvote 0
If this expected results
CityCategoryCount
XXX2
YYX1
YXX1
XXX1
YYX1

Then try
VBA Code:
Sub test()
    Dim a As Variant, i, ii, x
    a = Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 3)
    With CreateObject("scripting.dictionary")
        For i = 2 To UBound(a)
            If a(i, 1) <> "" Then
                If Not .exists(a(i, 3)) Then
                    .Add (a(i, 3)), Array(a(i, 1), a(i, 2), 1)
                Else
                    x = .Item(a(i, 3))
                    x(0) = x(0): x(1) = x(1): x(2) = x(2) + 1
                    .Item(a(i, 3)) = x
                End If
            End If
        Next
        Cells(1, 5) = Cells(1, 1): Cells(1, 6) = Cells(1, 2): Cells(1, 7) = "Count"
        Cells(2, 5).Resize(.Count, 3) = Application.Transpose(Application.Transpose(.Items))
    End With
End Sub
Thanks Sir; I do appreciate that, but

I just make a simple example, If I have many columns in source sheet (not only these 3 columns), how can I edit in your code to fit my table.
 
Upvote 0
OK
VBA Code:
 a = Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 3)
3 change it as per you columns NO
Then
if City & Category will go to a(i,1) & a(i,2)
a(i,3) for Receipt no.

so follow then in the new array (a)
Hope this can helps
 
Upvote 0
OK
VBA Code:
 a = Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 3)
3 change it as per you columns NO
Then
if City & Category will go to a(i,1) & a(i,2)
a(i,3) for Receipt no.

so follow then in the new array (a)
Hope this can helps
Thanks Again
And what about these

Cells(1, 5) = Cells(1, 1): Cells(1, 6) = Cells(1, 2): Cells(1, 7) = "Count"
Cells(2, 5).Resize(.Count, 3) = Application.Transpose(Application.Transpose(.Items))
 
Upvote 0
Cells(1, 5) = Cells(1, 1): Cells(1, 6) = Cells(1, 2): Cells(1, 7) = "Count"
Cells(2, 5).Resize(.Count, 3) = Application.Transpose(Application.Transpose(.Items))
Depends on the expected result
 
Upvote 0
Depends on the expected result
What I understood that the first line for header names in the target sheet
but the second line retrieve 0, I have the actual count column in my sheet in starting from cell L12 to L15

Can you please adjust the last line when copying data to include only count figures not category or city
 
Upvote 0
Replace with
VBA Code:
  Cells(12, 12).Resize(.Count) = Application.Index(Application.Transpose(Application.Transpose(.Items)), 0, 3)
 
Upvote 0

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

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