VBA: unique list in second column based on unique value in first column

kirankoushik

New Member
Joined
Feb 19, 2021
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I am a beginner to excel VBA coding and am trying to execute a task.
I have been struggling with a way to code in VBA the below requirement. Request your help in this regard.

I am trying to count the total of :

For every unique keyword in col C (excluding blanks), the total unique values in col E (including blanks)

1615991464716.png


From the above image:

For Key 1 in col C - unique values in Col E = 4

1615991737721.png


Thanks in Advance.

Kiran Koushik
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Maybe...

Array formula
=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET(A2:A36,ROW(A2:A36)-ROW(A2),0,1)),IFERROR(MATCH(E2:E36,E2:E36,0),ROWS(E2:E36))),ROW(E2:E36)-ROW(E2)+1),1))
confirmed with Ctrl+Shift+Enter, not just Enter

M.
 
Upvote 0
EDIT

New version
=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET(A2:A36,ROW(A2:A36)-ROW(A2),0,1)),IFERROR(MATCH(E2:E36,E2:E36,0),ROWS(E2:E36)+1)),ROW(E2:E36)-ROW(E2)+1),1))

M.
 
Upvote 0
EDIT

New version
=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET(A2:A36,ROW(A2:A36)-ROW(A2),0,1)),IFERROR(MATCH(E2:E36,E2:E36,0),ROWS(E2:E36)+1)),ROW(E2:E36)-ROW(E2)+1),1))

M.

Hello Marcelo,

Thank you for helping me.

I tried the above formula and am getting an incorrect value of 8. Maybe I am not communicating the requirement correctly. Please find below image. I am looking for answer 23.

Also, if I can achieve it using VBA it would be helpful as there are other calculations in the sheet and have an active module for the excel.

Thank you,
kiran koushik

1615997253465.png
 
Upvote 0
Book1.xlsx
ABCDEFGHIJKLMN
1Col ACol BCol CCol DCol ECol FResult
21Irrelevant DataKey 1 Irrelevant Dataval 1Irrelevant Data8
32Irrelevant DataKey 2Irrelevant Dataval 3Irrelevant Data
43Irrelevant DataKey 3Irrelevant Dataval 4Irrelevant Data
51Irrelevant DataKey 4Irrelevant Dataval 5Irrelevant Data
62Irrelevant DataKey 5Irrelevant Dataval 6Irrelevant Data
76Irrelevant DataKey 6Irrelevant Dataval 2Irrelevant Data
87Irrelevant DataKey 7Irrelevant Dataval 7Irrelevant Data
98Irrelevant DataKey 5Irrelevant Dataval 3Irrelevant Data
109Irrelevant DataKey 6Irrelevant DataIrrelevant Data
1110Irrelevant DataKey 1 Irrelevant Dataval 2Irrelevant Data
121Irrelevant DataKey 6Irrelevant Dataval 7Irrelevant Data
131Irrelevant DataKey 1 Irrelevant Dataval 2Irrelevant Data
142Irrelevant DataKey 6Irrelevant DataIrrelevant Data
153Irrelevant DataIrrelevant DataIrrelevant Data
161Irrelevant DataIrrelevant DataIrrelevant Data
172Irrelevant DataKey 2Irrelevant Dataval 3Irrelevant Data
186Irrelevant DataKey 3Irrelevant Dataval 4Irrelevant Data
197Irrelevant DataKey 7Irrelevant Dataval 5Irrelevant Data
208Irrelevant DataKey 7Irrelevant DataIrrelevant Data
219Irrelevant DataKey 2Irrelevant DataIrrelevant Data
2210Irrelevant DataKey 1 Irrelevant DataIrrelevant Data
231Irrelevant DataKey 4Irrelevant Dataval 2Irrelevant Data
241Irrelevant DataKey 3Irrelevant DataIrrelevant Data
252Irrelevant DataIrrelevant DataIrrelevant Data
263Irrelevant DataIrrelevant DataIrrelevant Data
271Irrelevant DataKey 4Irrelevant DataIrrelevant Data
282Irrelevant DataKey 3Irrelevant Dataval 1Irrelevant Data
296Irrelevant DataKey 7Irrelevant DataIrrelevant Data
307Irrelevant DataKey 5Irrelevant DataIrrelevant Data
318Irrelevant DataKey 1 Irrelevant Dataval 3Irrelevant Data
329Irrelevant DataKey 4Irrelevant Dataval 6Irrelevant Data
3310Irrelevant DataKey 3Irrelevant Dataval 5Irrelevant Data
341Irrelevant DataKey 4Irrelevant Dataval 6Irrelevant Data
359Irrelevant DataKey 5Irrelevant DataIrrelevant Data
3610Irrelevant DataKey 4Irrelevant DataIrrelevant Data
Sheet1
Cell Formulas
RangeFormula
H2H2=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET(A2:A36,ROW(A2:A36)-ROW(A2),0,1)),IFERROR(MATCH(E2:E36,E2:E36,0),ROWS(E2:E36)+1)),ROW(E2:E36)-ROW(E2)+1),1))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Hello Marcelo,

Thank you for helping me.

I tried the above formula and am getting an incorrect value of 8. Maybe I am not communicating the requirement correctly. Please find below image. I am looking for answer 23.

Also, if I can achieve it using VBA it would be helpful as there are other calculations in the sheet and have an active module for the excel.

Thank you,
kiran koushik

I thought you wanted the total filtering by Key 1, Key 2 ... etc

Try this
Pasta1
HI
1KeysUnique Count
2Key 14
3Key 22
4Key 34
5Key 44
6Key 53
7Key 63
8Key 73
9Total23
Plan8
Cell Formulas
RangeFormula
I2:I8I2=SUM(IF(FREQUENCY(IF(C$2:C$36=H2,IFERROR(MATCH(E$2:E$36,E$2:E$36,0),ROWS(E$2:E$36)+1)),ROW(E$2:E$36)-ROW(E$2)+1),1))
I9I9=SUM(I2:I8)
Press CTRL+SHIFT+ENTER to enter array formulas.


M.
 
Upvote 0
I thought you wanted the total filtering by Key 1, Key 2 ... etc

Try this
Pasta1
HI
1KeysUnique Count
2Key 14
3Key 22
4Key 34
5Key 44
6Key 53
7Key 63
8Key 73
9Total23
Plan8
Cell Formulas
RangeFormula
I2:I8I2=SUM(IF(FREQUENCY(IF(C$2:C$36=H2,IFERROR(MATCH(E$2:E$36,E$2:E$36,0),ROWS(E$2:E$36)+1)),ROW(E$2:E$36)-ROW(E$2)+1),1))
I9I9=SUM(I2:I8)
Press CTRL+SHIFT+ENTER to enter array formulas.


M.

Yes, Actually I wanted the total by filtering Key 1, Key 2,.. Key 7 .

i.e. Answer 23 in single cell

But the initial formula is giving answer 8.

Is there any way to incorporate the same in existing VBA module ?
 
Upvote 0
Yes, Actually I wanted the total by filtering Key 1, Key 2,.. Key 7 .

i.e. Answer 23 in single cell

But the initial formula is giving answer 8.

Is there any way to incorporate the same in existing VBA module ?

A solution using an Array Formula
=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET(A2:A36,ROW(A2:A36)-ROW(A2),0,1)),IF(C2:C36<>"",MATCH(C2:C36&"|"&E2:E36,C2:C36&"|"&E2:E36,0))),ROW(E2:E36)-ROW(E2)+1),1))
Ctrl+Shift+Enter


VBA

VBA Code:
Sub aTest()
    Dim rRngVisible, rRow As Range, lTotal As Long
    Dim dic As Object
    
    Set dic = CreateObject("Scripting.Dictionary")
    dic.CompareMode = vbTextCompare
    
    Set rRngVisible = Range("A2:F36").SpecialCells(xlCellTypeVisible)
    For Each rRow In rRngVisible.Rows
        If rRow.Cells(1, 3) <> "" Then
            dic(rRow.Cells(1, 3).Value & "|" & rRow.Cells(1, 5).Value) = ""
        End If
    Next rRow
    lTotal = dic.Count
    MsgBox lTotal 'Just for check
End Sub

M.
 
Upvote 0
Solution
A solution using an Array Formula
=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET(A2:A36,ROW(A2:A36)-ROW(A2),0,1)),IF(C2:C36<>"",MATCH(C2:C36&"|"&E2:E36,C2:C36&"|"&E2:E36,0))),ROW(E2:E36)-ROW(E2)+1),1))
Ctrl+Shift+Enter


VBA

VBA Code:
Sub aTest()
    Dim rRngVisible, rRow As Range, lTotal As Long
    Dim dic As Object
   
    Set dic = CreateObject("Scripting.Dictionary")
    dic.CompareMode = vbTextCompare
   
    Set rRngVisible = Range("A2:F36").SpecialCells(xlCellTypeVisible)
    For Each rRow In rRngVisible.Rows
        If rRow.Cells(1, 3) <> "" Then
            dic(rRow.Cells(1, 3).Value & "|" & rRow.Cells(1, 5).Value) = ""
        End If
    Next rRow
    lTotal = dic.Count
    MsgBox lTotal 'Just for check
End Sub

M.

Thanks a lot Marcelo for your time. :)

Both formula and VBA code work perfectly. (y)
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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