VBA Advanced Filter help/ Dynamic Function

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

My Code is giving me Correct result, how to achieve same result via advanced filter or Dynamic Function to reduce Code,
In below I have taken only Two Situation there may be 10-15 like this.

ABCSituation 1 , Include all Criteria ListSituation 2 , Exclude Column J List
List1List2List3Criteria List 1Criteria List 2Criteria List 3Criteria List 1(Exclude)Criteria List 2Criteria List 3
ZZZCCCNNNAAAMMMSSSAAAMMMSSS
AAANNNWWWBBBNNNTTTBBBNNNTTT
CCCGGGIIICCCPPPWWWCCCPPPWWW
WWWRRRVVVRRRVVVRRRVVV
JJJAAAUUUWWWWWW

Situation1
Column A, B, C, are my input data,
Situation1, Column F,G,H Are my filter Criteria List.{Filter In}

Situation2, Same situation but exclude Column J Criteria list (Filter Out)
Situation2 - Column J,K,L Are my filter Criteria List.


VBA Code:
Option Explicit
Sub Count_as_Per_Criteria()

Dim ar1 As Variant
Dim ar2 As Variant
Dim ar3 As Variant

ar1 = Sheet1.Range("F3", Range("F" & Rows.Count).End(xlUp))
ar2 = Sheet1.Range("G3", Range("G" & Rows.Count).End(xlUp))
ar3 = Sheet1.Range("H3", Range("H" & Rows.Count).End(xlUp))

Dim i As Long, Situation1 As Long, lr As Long
lr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row

'Situaction 1 Include all Criteria  ' Code is working
For i = 2 To lr
    If IsNumeric(Application.Match(Sheet1.Cells(i, 1).Value, ar1, 0)) Then
         If IsNumeric(Application.Match(Sheet1.Cells(i, 2).Value, ar2, 0)) Then
            If IsNumeric(Application.Match(Sheet1.Cells(i, 3).Value, ar3, 0)) Then
                    Sheet1.Cells(i, 1).Resize(1, 3).Font.Color = vbBlue
                Situation1 = Situation1 + 1
            End If
        End If
    End If
Next i

MsgBox Situation1


'----------Situation2 Count--------------------

ar1 = Sheet1.Range("J3", Range("J" & Rows.Count).End(xlUp))
ar2 = Sheet1.Range("K3", Range("K" & Rows.Count).End(xlUp))
ar3 = Sheet1.Range("L3", Range("L" & Rows.Count).End(xlUp))

Dim Situation2 As Long

For i = 2 To lr
    If IsNumeric(Application.Match(Sheet1.Cells(i, 1).Value, ar1, 0)) = False Then 
         If IsNumeric(Application.Match(Sheet1.Cells(i, 2).Value, ar2, 0)) Then
            If IsNumeric(Application.Match(Sheet1.Cells(i, 3).Value, ar3, 0)) Then
                    Sheet1.Cells(i, 1).Resize(1, 3).Font.Color = vbRed
                Situation2 = Situation2 + 1
            End If
        End If
    End If
Next i

MsgBox Situation2


End Sub

Thanks
mg
 

Attachments

  • Criteria_List_Table.png
    Criteria_List_Table.png
    3.5 KB · Views: 5

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

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