VBA to delete all rows in a sheet except rows with specific cells in specific column

Dustinkli

Board Regular
Joined
Mar 26, 2019
Messages
62
Office Version
  1. 365
  2. 2011
Platform
  1. Windows
I have a spreadsheet and I need to delete all rows that do NOT have specified values in a column. Essentially I would like to delete all rows that do not contain specific words from a preserve list. The cells in the column will have full sentences in them so I want it to just isolate if the specified word exists anywhere in the cell and NOT delete that row.

I have two working ways to do something similar to this:

Method 1:
VBA Code:
Sub deletexptlst()
Dim List As Variant
    Dim LR As Long
    Dim r As Long
    List = Array("Value 1", "Value 2", "Value 3", "Value 4")
    LR = Range("E" & Rows.Count).End(xlUp).Row
    For r = LR To 2 Step -1
        If IsError(Application.Match(Range("E" & r).Value, List, False)) Then
            Rows(r).Delete
        End If
    Next r

End Sub

Method 1 works by making an array of the values I want to preserve, but it does not seem to be able (or I can't figure out how) to make it wildcard based so that the value can exist anywhere in the cell.


Method 2:
VBA Code:
Sub delallexcept()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Not Range("E" & i).Value Like "*Value*" Then Rows(i).Delete
Next i
 ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With

End Sub

Method 2 works with the wildcard "*" but I can't figure out how to add additional values to not be deleted.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this:
VBA Code:
Sub MyDeleteList()

    Dim List As Variant
    Dim LR As Long
    Dim r As Long
    Dim i As Long
    Dim keep As Boolean
    
    Application.ScreenUpdating = False
    
'   Store values to look for
    List = Array("Value 1", "Value 2", "Value 3", "Value 4")
    
'   Find last row with data in column E
    LR = Range("E" & Rows.Count).End(xlUp).Row
    
'   Loop through all rows, from bottom to top
    For r = LR To 2 Step -1
'       Set keep variable to false to start
        keep = False
'       Loop through all values in array
        For i = LBound(List) To UBound(List)
'           See if each value found in row
            If InStr(Range("E" & r).Value, List(i)) > 0 Then
'               If found, set keep to true and exit loop
                keep = True
                Exit For
            End If
        Next i
'       Check to see if we should delete row
        If keep = False Then Rows(r).Delete
    Next r

    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"
    
End Sub
 
Upvote 0
That looks to work! Thank you very much!

How did you get it so the array list would allow for the variable anywhere in the cell?
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,635
Messages
6,125,945
Members
449,275
Latest member
jacob_mcbride

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