Remove everything but multiple text OR remove row if cells contact certain text value

godzilla185

New Member
Joined
Sep 27, 2021
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi everyone, if anyone can help me on the below.

I am trying to get a macro that will delete rows if certain cells contact certain text, or even better, delete all rows except ones containing certain text.

The below code works great only problem is it only works on 1 variable "name1", but I need it to remove multiple names (10+)... If there is a simple way for me to elaborate on this code it would be easiest for me, but I tried adding OR/AND to the "name1" and it doesn't work.


Sub pivot()

' Filter_out_text Macro
'
Application.ScreenUpdating = False
Dim i As Long
LR = Range("O" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
If Range("O" & i) = "name1" Then
Rows(i).EntireRow.Delete
End If
Next i
Application.ScreenUpdating = True
End Sub
 
Try this:
VBA Code:
Sub Filter_Me_Please_Array()
'Modified  9/27/2021  1:15:54 PM  EDT
Dim Del As Variant
Del = Array("Mary", "Mark", "Alex", "John") ' Modify search values
Dim lastrow As Long
Dim c As Long
Dim counter As Long
c = "15" ' Column Number Modify this to your need
lastrow = Cells(Rows.Count, c).End(xlUp).Row

With ActiveSheet.Cells(1, c).Resize(lastrow)
    .AutoFilter 1, Del, Operator:=xlFilterValues
    counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
    If counter > 1 Then
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
 
    Else
        MsgBox "No values found"
    End If
    .AutoFilter
End With
End Sub
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

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