vba help - filter via dictionary

Mallesh23

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

How to filter Data using Dictionary Keys. I got this code in this forum only.
Here I want to filter dictionary Keys and delete the the Data.


Exclude List:=> Case "Dhoni", "Sachin", "Virat"
.AutoFilter Field:=6, Criteria1:=d.keys(), Operator:=xlFilterValues Getting Error at this line.

VBA Code:
Sub AF()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  With Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
    a = .Columns(1).Value
    For i = 2 To UBound(a)
      Select Case a(i, 1)
        Case "Dhoni", "Sachin", "Virat"
        'Case Else: d(a(i, 1) & "") = 1
        Case Else: d(a(i, 1)) = 1
      End Select
    Next i
    .AutoFilter Field:=6, Criteria1:=d.keys(), Operator:=xlFilterValues   ' Filter not working here
  End With
  
  Range("a1").CurrentRegion.Offset(1).EntireRow.Delete
  
  
End Sub

Below is a Sample data Testing Above Code.

Book1
AB
1Name
2Sachinkeep
3Dhonikeep
4Viratkeep
5KohliDelete
6BumrahDelete
7GayleDelete
8PontingDelete
9GilchristDelete
10David WarnerDelete
Sheet1



Thanks
mg
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Your filter range needs to include the header row in row 1, and you can't filter on Field 6 if you only have one column, so:

VBA Code:
Sub AF()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  With Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    a = .Columns(1).Value
    For i = 2 To UBound(a)
      Select Case a(i, 1)
        Case "Dhoni", "Sachin", "Virat"
        'Case Else: d(a(i, 1) & "") = 1
        Case Else: d(a(i, 1)) = 1
      End Select
    Next i
    .AutoFilter Field:=1, Criteria1:=d.keys, Operator:=xlFilterValues   ' Filter not working here
  End With
  
  Range("a1").CurrentRegion.Offset(1).EntireRow.Delete
  
End Sub
 
Upvote 0
Hi Rory,

Thanks for your help, Code worked in below two situation.

When to use &"" Operator.?....

VBA Code:
'Case Else: d(a(i, 1)[B] & "") = 1  'When to use & "", [/B]I didn't see any change in output
        Case Else: d(a(i, 1)) = 1



Thanks
mg
 
Upvote 0
You only need that if you specifically need to coerce the value into a String - eg the cell contains 1 and you want "1" as the key for some reason.
 
Upvote 0
Hi Rory,

Perfect ! Thanks once again for your help, This cleared my doubt. (y) ?


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,224,011
Messages
6,175,928
Members
452,684
Latest member
RRaively1

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