VBA to delete rows that DON'T contain certain text

Ashe

New Member
Joined
Dec 2, 2023
Messages
9
Office Version
  1. 2021
Platform
  1. Windows
I have data in column B that has multiple codes in it. A majority of the codes aren't needed for what I'm calculating, and instead of writing all the codes what I want deleted, is there a way to code what I codes DON'T want to delete and delete all other rows of data?

Here are the codes I want to KEEP:
LN_OTHER/MISC
LN_LEARNING_STAFF
LN_PITCLASSRM_TRAING
LN_TDRCLASSRM_TRAING

Here's what I currently have for my code, which deletes specified codes:
'Delete any irrelevant data
Dim LastRow As Long, n As Long
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row

For n = LastRow To 1 Step -1
If Range("B" & n).Value = "Irrelevant_Code_1" Or _
Range("B" & n).Value = "Irrelevant_Code_2" Or _
Range("B" & n).Value = "Irrelevant_Code_3" Or _
(Etc etc for a lot of rows...) Then
Rows ( n ) .Delete
End If
Next
I could use an autofilter, but I'd prefer to omit the data altogether as I may need to further manipulate the data as I develop this document.

Any help is greatly appreciated! Thanks in advance :)
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Since there are 4 codes you want to keep, how about something structured like this:
VBA Code:
Dim LastRow As Long, n As Long
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row

For n = LastRow To 1 Step -1
    If Range("B" & n).Value = "Relevant_Code_1" Or _
        Range("B" & n).Value = "Relevant_Code_2" Or _
        Range("B" & n).Value = "Relevant_Code_3" Or _
        Range("B" & n).Value = "Relevant_Code_4" Then
        'Do nothing
    Else
        Rows(n).Delete
    End If
Next
 
Upvote 1
Solution
Since there are 4 codes you want to keep, how about something structured like this:
VBA Code:
Dim LastRow As Long, n As Long
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row

For n = LastRow To 1 Step -1
    If Range("B" & n).Value = "Relevant_Code_1" Or _
        Range("B" & n).Value = "Relevant_Code_2" Or _
        Range("B" & n).Value = "Relevant_Code_3" Or _
        Range("B" & n).Value = "Relevant_Code_4" Then
        'Do nothing
    Else
        Rows(n).Delete
    End If
Next
I feel so silly. 🤦‍♂️ That works perfect, thank you so much!!
 
Upvote 0
Code:
Sub AAAAA()
Dim datArr, keeper As String, i As Long, rng As Range
datArr = Range("B1:B" & Cells(Rows.Count, 2).End(xlUp).Row).Value
keeper = "LN_OTHER/MISC|LN_LEARNING_STAFF|LN_PITCLASSRM_TRAING|LN_TDRCLASSRM_TRAING"
For i = LBound(datArr) To UBound(datArr)
If InStr(keeper, datArr(i, 1)) = 0 Then
    If Not rng Is Nothing Then
        Set rng = Union(rng, Cells(i, 1)).EntireRow
            Else
        Set rng = Cells(i, 1).EntireRow
    End If
End If
Next i
rng.Delete
End Sub
 
Upvote 0
You are welcome.
Glad we could help!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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