VBA code for Distinct Count

Michael Fowler

New Member
Joined
Jan 23, 2024
Messages
28
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Need help with two things ... (1) VBA code for Distinct Count of Values in a Column, and (2) VBA code for Distinct Count of Values in a Column, but with a condition from another Column (in effect, a Distinct CountIf).

Thanks in advance.
 
  1. Hmm, the suggestions offered have been based on what is shown in your profile. If you need your question answered for different versions it obviously would be best to state that in the first post of a thread.
  2. Do you need this solution for work or for home?
  3. If you are on 2016 in the office and 2021 at home, when are you using the 365 shown in your profile?
Thanks for pointing that out Peter. I don't remember selecting 365 on my profile, so I don't know if that was a default selection or not. Have corrected now, changing to both 2021 and 2016 in my profile to cater for both home and work systems. In answer to your question ... immediately I need this solution to work in 2016, but it would also be good if it worked in 2021. Cheers, Michael.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
No, 365 is not a default so you must have accidentally set that but at least you have it right now. :)

If you were interested in a solution using a permanent formula you could use this which should count the unique values in rows 2:1000 even if any rows in that range get deleted.

Excel Formula:
=SUMPRODUCT((INDEX('Subledger Data'!H:H,2):INDEX('Subledger Data'!H:H,1000)<>"")/COUNTIF(INDEX('Subledger Data'!H:H,2):INDEX('Subledger Data'!H:H,1000),INDEX('Subledger Data'!H:H,2):INDEX('Subledger Data'!H:H,1000)&""))

However, if you do want to go with a vba solution you could try something like this. My code uses the last row used in column H of 'Subledger Data'. I'm not sure if that was your intention. If not you can modify the code for a different calculation of 'Lastrow'

VBA Code:
Sub Test2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long, Lastrow As Long, MyCount As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Subledger Data")
    Lastrow = .Range("H" & Rows.Count).End(xlUp).Row
    a = .Range("H2", .Range("H" & Lastrow)).Value
  End With
  For i = 1 To UBound(a)
    If Len(a(i, 1)) > 0 Then d(a(i, 1)) = 1
  Next i
  MyCount = d.Count
  MsgBox MyCount
End Sub
 
Upvote 0
No, 365 is not a default so you must have accidentally set that but at least you have it right now. :)

If you were interested in a solution using a permanent formula you could use this which should count the unique values in rows 2:1000 even if any rows in that range get deleted.

Excel Formula:
=SUMPRODUCT((INDEX('Subledger Data'!H:H,2):INDEX('Subledger Data'!H:H,1000)<>"")/COUNTIF(INDEX('Subledger Data'!H:H,2):INDEX('Subledger Data'!H:H,1000),INDEX('Subledger Data'!H:H,2):INDEX('Subledger Data'!H:H,1000)&""))

However, if you do want to go with a vba solution you could try something like this. My code uses the last row used in column H of 'Subledger Data'. I'm not sure if that was your intention. If not you can modify the code for a different calculation of 'Lastrow'

VBA Code:
Sub Test2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long, Lastrow As Long, MyCount As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  With Sheets("Subledger Data")
    Lastrow = .Range("H" & Rows.Count).End(xlUp).Row
    a = .Range("H2", .Range("H" & Lastrow)).Value
  End With
  For i = 1 To UBound(a)
    If Len(a(i, 1)) > 0 Then d(a(i, 1)) = 1
  Next i
  MyCount = d.Count
  MsgBox MyCount
End Sub
Thanks Peter, I'll give those a try and let you know how it goes. I do appreciate your help. Cheers, Michael.
 
Upvote 0
Thanks Peter, I'll give those a try and let you know how it goes. I do appreciate your help. Cheers, Michael.
Yes Peter, your VBA code did work, so thanks very much for that. :) Are you also able to help me with the second part of my query ? That is, VBA code for Distinct Count of Values in a Column, but with a condition from another Column. So I need to do a count on the Column you've already helped me with ('Subledger Data'!H:H) by Funder 1, Funder 2, etc. So a Distinct Count on column H:H by Funders in another column.
 
Upvote 0
Yes Peter, your VBA code did work, so thanks very much for that. :) Are you also able to help me with the second part of my query ? That is, VBA code for Distinct Count of Values in a Column, but with a condition from another Column. So I need to do a count on the Column you've already helped me with ('Subledger Data'!H:H) by Funder 1, Funder 2, etc. So a Distinct Count on column H:H by Funders in another column.
That sounds like you are looking for multiple results rather than a single result?
Could you make up a small set of dummy data (say about 15 rows with 3 Funders) and post that and the expected results with XL2BB?
That way we can see how the data is arranged (eg if sorted or not), copy it for testing and know where the columns are and where the results should be and what they would look like.
 
Upvote 0
That sounds like you are looking for multiple results rather than a single result?
Could you make up a small set of dummy data (say about 15 rows with 3 Funders) and post that and the expected results with XL2BB?
That way we can see how the data is arranged (eg if sorted or not), copy it for testing and know where the columns are and where the results should be and what they would look like.
Hi Peter. Firstly I needed a single result, which you've solved for me. Secondly, I now need multiple results, which is not number of transactions by Funder, but number of different Clients by Funder. In other words, for each Funder a Distinct Client Count. I have a small data sample ready, but am not familiar with XL2BB. Please advise.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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