VBA Code to get distinct count with muliple criteria

Wafee

Board Regular
Joined
May 27, 2020
Messages
104
Office Version
  1. 2013
Platform
  1. Windows
Can some one help me with a VBA code that gives distinct count of data in in "I" Column of sheet1 (dynamic) after filtering "Temp" and "Tem" in A column and "MX" in E column. I have a code as below which gives distinct count from "I" column but not able to add the filters. Would be great if get a code simpler than this.

Sub Dcount()
Dim Lastrow As Long
Rng As Range
List As Object
Listcount As Long
Lastrow = Cells(Rows.Count,"A").End(xlUp).Row
Set List = Createobject("Scripting.Dictionary)
For Each Rng In Range("I2:I", & Lastrow)
If Not List.Exists(Rng.Value) Then List.Add Rng.Value, Nothing
Next
Listcount = List.Count
Range("M2").Value = ListCount

End Sub
 
What is the actual value in M2 when you get the error?
It the count. 223 in my case actually. It is the distinct count from column I. Now I also want that list of 223 unique list along with the count.
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Oops, it should have been
VBA Code:
Range("S2").Resize(List.Count).Value = Application.Transpose(List.Keys)
 
Upvote 0
Oops, it should have been
VBA Code:
Range("S2").Resize(List.Count).Value = Application.Transpose(List.Keys)
Works Perfectly mate. You are a legend and I promise this will be the last thing I am gonna ask. Can we able to get sum of hours which is in in R column for the above list alone?
 
Upvote 0
How about
VBA Code:
Sub Dcount()
Dim Lastrow As Long
Dim Rng As Range
Dim List As Object
Dim Listcount As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set List = CreateObject("Scripting.Dictionary")
With Range("A1:I1")
   .AutoFilter 1, "Temp", xlOr, "tem"
   .AutoFilter 5, "MX"
End With
For Each Rng In Range("I2:I" & Lastrow).SpecialCells(xlVisible)
   List(Rng.Value) = List(Rng.Value) + Rng.Offset(, 9).Value
Next Rng
ActiveSheet.AutoFilterMode = False
Listcount = List.Count
Range("M2").Value = Listcount
Range("S2").Resize(List.Count, 2).Value = Application.Transpose(Array(List.Keys, List.Items))

End Sub
 
Upvote 0
How about
VBA Code:
Sub Dcount()
Dim Lastrow As Long
Dim Rng As Range
Dim List As Object
Dim Listcount As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Set List = CreateObject("Scripting.Dictionary")
With Range("A1:I1")
   .AutoFilter 1, "Temp", xlOr, "tem"
   .AutoFilter 5, "MX"
End With
For Each Rng In Range("I2:I" & Lastrow).SpecialCells(xlVisible)
   List(Rng.Value) = List(Rng.Value) + Rng.Offset(, 9).Value
Next Rng
ActiveSheet.AutoFilterMode = False
Listcount = List.Count
Range("M2").Value = Listcount
Range("S2").Resize(List.Count, 2).Value = Application.Transpose(Array(List.Keys, List.Items))

End Sub
Thanks again Fulff, Here we are finding the list and making it in to an array. For the values in the array I want sum of values that are in R column. To re-iterarate I want Sum from R column for the above list or array which we obtained. So I am thinking on the lines of looping through each item from the list and getting their value from R column.
 
Upvote 0
That does give you the sum of each value in the list.
 
Upvote 0
I want to get total sum from R colum for all the items from the array with different criteria. So my ask here is once we figured out the list or array of values which are from I column can we filter "Yes" from K column and get the total sum from R column.
 
Upvote 0
That is a totally different question & so needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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