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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
VBA Code:
For Each Rng In Range("I2:I", & Lastrow).Specialcells(xlVisible)
 
Upvote 0
How about
VBA Code:
For Each Rng In Range("I2:I", & Lastrow).Specialcells(xlVisible)
Hi Fluff,
Sorry if my question was confusing but code has to filter and select "Temp" and "Tem" in A column and "MX" in E column and the calculate the distinct count.
 
Upvote 0
Try:
VBA Code:
Sub Dcount()
    Application.ScreenUpdating = False
    With Cells(1, 1)
        .CurrentRegion.AutoFilter Field:=1, Criteria1:="=Tem", Operator:=xlOr, Criteria2:="=Temp"
        .CurrentRegion.AutoFilter Field:=5, Criteria1:="MX"
    End With
    Range("M2") = [subtotal(103,A:A)] - 1
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
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) = Empty
Next Rng
ActiveSheet.AutoFilterMode = False
Listcount = List.Count
Range("M2").Value = Listcount

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) = Empty
Next Rng
ActiveSheet.AutoFilterMode = False
Listcount = List.Count
Range("M2").Value = Listcount

End Sub

Thank you that worked Perfectly, except at With Range("A1:I1") where used with Cells(1,1) instead.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Try:
VBA Code:
Sub Dcount()
    Application.ScreenUpdating = False
    With Cells(1, 1)
        .CurrentRegion.AutoFilter Field:=1, Criteria1:="=Tem", Operator:=xlOr, Criteria2:="=Temp"
        .CurrentRegion.AutoFilter Field:=5, Criteria1:="MX"
    End With
    Range("M2") = [subtotal(103,A:A)] - 1
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub

Hi Mumps,

Can you help me with below requirement.

With the same filter in field 1, can we change criteria in field 5 to "NX" rather than "MX".
 
Upvote 0
You're welcome & thanks for the feedback.

Hi Fluff

Can you help me with below requirement.

With the same filter in field 1, can we change criteria in field 5 to "NX" rather than "MX". Thank you in advance.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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