Delete entire row IF cell contains one of values from range

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello dear community,

I use following VBA to delete entire row if cell contains "6" or "7"

But now I want to add a lot more criterias. Is it possible to delete entire row if cells A40:A3000 contains any value from range B1:B40 ?

VBA Code:
    Dim rng As Range
    Set rng = ActiveSheet.Range("A40:A3000")
    
    For i = rng.Cells.Count To 1 Step -1
        If rng.Item(i).Value = "6" Or rng.Item(i).Value = "7" Then
            rng.Item(i).EntireRow.Delete
        End If
    Next i
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this on a copy of your data.

VBA Code:
Option Explicit
Sub Akbarov()
    Dim ws As Worksheet, Arr, i As Long
    Set ws = ActiveSheet
    Arr = ws.Range("B1:B40").Value
    Arr = Application.Transpose(Application.Index(Arr, 0, 1))
    
    For i = LBound(Arr) To UBound(Arr)
        Arr(i) = CStr(Arr(i))
    Next i
    
    With ws.Range("A40:A3000")
        .AutoFilter 1, Array(Arr), 7
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        .AutoFilter
    End With

End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub Delete_Me_With_Case()
'Modified  10/6/2022  7:28:09 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
    
    For i = 3000 To 40 Step -1
        Select Case Cells(i, 1).Value
            Case Range("B1") To Range("B40")
                Rows(i).Delete
        End Select
    Next
   Application.ScreenUpdating = True

   MsgBox "Done"
End Sub
 
Upvote 0
Glad we were able to help you.
Come back here to Mr. Excel next time you need additional assistance.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

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