Delete row if multiple conditions are met

abc_xyz

New Member
Joined
Jan 12, 2022
Messages
47
Office Version
  1. 2016
Platform
  1. Windows
I am trying to create a code which checks if the Dictionary value exits or not in the Column A. If it does then check the value for 24th Column of that particular row. If the value in the 24th Column is not 7 or 4 then delete the entire row.

No error message is shown when I try to run the below code. However, the results are not as per the requirements. It simply deletes the rows when it finds the dictionary value.

VBA Code:
Sub filter_test()

Dim Cl As Range, Rng As Range
Dim Dic As Object

Set Dic = CreateObject("scripting.dictionary")


'Add Key words to dictionary
    
    With Sheets("Criteria.Temp")
   
        For Each Cl In .Range("L2", .Range("L" & Rows.Count).End(xlUp))
            
            Dic(Cl.Value) = Cl.Value
        
        Next Cl
        
    End With



'Delete key word's row from the Output sheet

    With Sheets("Output")
        
        For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
            
            If Dic.Exists(Cl.Value) Then
               
               If Cl.Offset(, 24) <> "7" Or Cl.Offset(, 24) <> "4" Then
                   If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
               End If
            
            End If
        
        Next Cl
    
    End With
       
     If Not Rng Is Nothing Then Rng.EntireRow.Delete

Set Dic = Nothing
Set Rng = Nothing


End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Change the below line from OR to AND
s/be
VBA Code:
If Cl.Offset(, 24) <> "7" And Cl.Offset(, 24) <> "4" Then
Assuming you want to delete the rows that contain something other than 7 and 4. ie you finish up with the rows that have 7 or 4.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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