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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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