Delete all rows not containing specific information in column A

mrmcoupe

New Member
Joined
Jan 22, 2017
Messages
17
Hi!

I have this code deleting all rows that do not contain the text "A#ABN" in column A. However, I also need to keep rows containing the value "A#UUW" also. Single delete works perfectly, but I struggle when adding more criteria. I´ve tried different versions of code two, but still noting:(

1) This one works fine:
Code:
Sub delete_row()    

Dim r As Long, lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For r = lr To 2 Step -1
        If InStr(Cells(r, 1), "A#ABN") = 0 Then Rows(r).Delete
        Next r
End Sub


2) This does not work:
Code:
Sub delete_row()    

Dim r As Long, lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For r = lr To 2 Step -1
        If InStr(Cells(r, 1), "A#ABN" [U][I]Or "A#UUW")[/I][/U] = 0 Then Rows(r).Delete
        Next r
End Sub







 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try:
Code:
If (InStr(Cells(r, 1), "A#ABN") = 0) Or (InStr(Cells(r, 1), "A#UUW") = 0) Then Rows(r).Delete
 
Upvote 0
Try:
Code:
If (InStr(Cells(r, 1), "A#ABN") = 0) Or (InStr(Cells(r, 1), "A#UUW") = 0) Then Rows(r).Delete

Hi Joe4,

thanks for reply! :)

This one seemed logic for me also, but I´ve tried this one as well, but then all rows are deleted. :/
 
Upvote 0
Can you post a handful of examples of the entries that might appear in the cells (some that will be deleted, and some that will not)?
 
Upvote 0
Try:
Code:
Sub DelRows()


    Dim x       As Long
    Dim arr()   As Variant
    
    arr = Cells(2, 1).Resize(Cells(Rows.Count, 1).End(xlUp).row).Value
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        If inster(arr(x, 1), "A#BN") + inster(arr(x, 1), "A#UW") = 0 Then arr(x, 1) = Null
    Next x
    
    Application.ScreenUpdating = False
    With Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2))
        .Value = arr
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
    
    Erase arr
    
End Sub
 
Upvote 0
I misread your original post. You want to NOT to delete the ones that contain that text (not delete them).

So all you have to do in the last code I posted is change the word OR to AND and it will work.
 
Upvote 0
Yes, perfect! That worked as I hoped. I think I tried a version of this, but I couldnt make it happen. Thank you @Joe4! :)

BTW: Sorry for late reply, been away for a couple of days!

brg
H
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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