Deleting Rows with Specific Word along the Same Column VBA

excelnoob1991

New Member
Joined
Jan 6, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi there,

It's my first time posting here; I apologise if I may not be following any guidelines.

I am trying to create a macro which helps delete any word in Column E.
For example, The word I would like to delete is "Cleaner" but in Column E, there are multiple selections such as BUILDING CLEANER, CLEANER L16- LAILA, FRANK CLEANER 2

I am interested to delete all the above. Any rows that have this specific word.
Tried searching for a solution, but I wasn't able to.
I have a header and my columns are from A to G.

Am aware a simple filter and delete would suffice, but I am trying to learn a bit more VBA knowledge so I can automate my repetitive work a little.

Thank you.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Please try this on a copy of your workbook. Run the macro with the sheet in question active.

VBA Code:
Option Explicit     '<-- Always use this - forces declaration of variables/avoids problems
Option Compare Text '<-- Use this to avoid upper/lower case text comparison issues
Sub Delete_Cleaners()

    'Declare variables by type
    Dim LRow As Long, i As Long, a As Variant, b As Variant
    
    'Find last row with data in column E
    LRow = Cells(Rows.Count, "E").End(xlUp).Row
    
    'Load values into array a
    a = Range("E2:E" & LRow)
    
    'Resize array b to match array a
    ReDim b(1 To UBound(a), 1 To 1)
    
    'Loop through array a and if elements contain "CLEANER" then put a 1 into array b
    For i = 1 To UBound(a)
        If a(i, 1) Like "*CLEANER*" Then b(i, 1) = 1
    Next i
    
    'Output array b to the sheet from cell H2
    Range("H2").Resize(UBound(b, 1)).Value = b
    
    'Check if any values contained "CLEANER" and if so, sort by column H & delete those rows
    i = WorksheetFunction.Sum(Columns("H"))
    If i > 0 Then
        Range("A2:H" & LRow).Sort Key1:=Range("H2"), order1:=xlAscending, Header:=xlNo  '<-- sort
        Range("H2").Resize(i).EntireRow.Delete                                          '<-- delete
    End If

End Sub
 
Upvote 0
It works!! THANK YOU!!!

It looks a tad bit confusing though hahah maybe cos I'm new.

I'm wondering if it's possible to add different elements.
I tried the below but not possible though.

Code:
For i = 1 To UBound(a)
        If a(i, 1) Like "*CLEANER*", "*TEMP*", "*VISITOR*" Then b(i, 1) = 1
    Next i
 
Upvote 0
Happy to help & welcome to the Forum :) (y)

One way to add elements is by using the OR operator, so you'd replace this:
VBA Code:
For i = 1 To UBound(a)
        If a(i, 1) Like "*CLEANER*" Then b(i, 1) = 1
Next i

With this:
VBA Code:
For i = 1 To UBound(a)
        If a(i, 1) Like "*CLEANER*" Or _
        a(i, 1) Like "*TEMP*" Or _
        a(i, 1) Like "*VISITOR*" Then b(i, 1) = 1
Next i
 
Upvote 0
Happy to help & welcome to the Forum :) (y)

One way to add elements is by using the OR operator, so you'd replace this:
VBA Code:
For i = 1 To UBound(a)
        If a(i, 1) Like "*CLEANER*" Then b(i, 1) = 1
Next i

With this:
VBA Code:
For i = 1 To UBound(a)
        If a(i, 1) Like "*CLEANER*" Or _
        a(i, 1) Like "*TEMP*" Or _
        a(i, 1) Like "*VISITOR*" Then b(i, 1) = 1
Next i
I could kiss you!!

THANK YOU SOOO MUCH!!!
Now to google other VBAs for Vlookups hahahaha

Thank you again Kevin!!
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,974
Members
449,095
Latest member
Mr Hughes

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