Delete rows based on list of different words/arguments

BexcelB

New Member
Joined
Jan 17, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a list of 25.000 products and some rows has to be deleted since they are not interestning.

Is is possible to create VBA code that goes trough the complete sheet and delete rows based on a list of different words/different arguments.
Product name often consist two words but the VBA code just have look on only one word.

Hope it makes sense.

Best regards
BexcelB
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
yes it is possible make sure to provide some sample data, maybe with like operator or instr or if it's more advanced maybe with Regex.
 
Upvote 0
yes it is possible make sure to provide some sample data, maybe with like operator or instr or if it's more advanced maybe with Regex.
F.ex. rows that contains Aronia and Arrangement should be deleted. But there can be many different words.

1673960413140.png
 
Upvote 0
VBA Code:
Sub Row_Deleter()
        '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _
        ALWAYS TEST YOUR CODE ON A SAMPLE OF DATA TO FIND BUGS
        Dim lr As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Dim k As Long
        Dim V, S
        Dim store As Long
        V = Array("Louis", "Becky") ' add the words you want to delete and remember that this is a CASE SENSITIVE search
        
        For k = lr To 2 Step -1
                For Each S In V
                  If InStr(1, Range("A" & k), S) > 0 Then
                    store = store + 1
                  End If
                Next S
                        If store >= 1 Then
                            Range("A" & k).EntireRow.Delete
                        End If
                    store = 0
        Next k
        
End Sub
 
Upvote 0
VBA Code:
Sub Row_Deleter()
        '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _
        ALWAYS TEST YOUR CODE ON A SAMPLE OF DATA TO FIND BUGS
        Dim lr As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Dim k As Long
        Dim V, S
        Dim store As Long
        V = Array("Louis", "Becky") ' add the words you want to delete and remember that this is a CASE SENSITIVE search
      
        For k = lr To 2 Step -1
                For Each S In V
                  If InStr(1, Range("A" & k), S) > 0 Then
                    store = store + 1
                  End If
                Next S
                        If store >= 1 Then
                            Range("A" & k).EntireRow.Delete
                        End If
                    store = 0
        Next k
      
End Sub
Thanks - this work absolutely perfect. Possible to search and delete words in more than one column? F.ex. from column A to J.
 
Upvote 0
Try this
VBA Code:
Sub Row_Deleter_2()
        '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _
        ALWAYS TEST YOUR CODE ON A SAMPLE OF DATA TO FIND BUGS
        Dim lr As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Dim k, i As Long
        Dim V, S
        Dim store As Long
        V = Array("Louis", "Becky") ' add the words you want to delete and remember that this is a CASE SENSITIVE search
        
        For i = 1 To 10 '10 is the column J change it accordingly
                    For k = lr To 2 Step -1
                            For Each S In V
                              If InStr(1, Cells(k, i), S) > 0 Then
                                store = store + 1
                              End If
                            Next S
                                    If store >= 1 Then
                                        Cells(k, i).EntireRow.Delete
                                    End If
                                store = 0
                    Next k
            lr = Range("A" & Rows.Count).End(xlUp).Row
        Next i
End Sub
 
Upvote 0
Solution
Try this
VBA Code:
Sub Row_Deleter_2()
        '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _
        ALWAYS TEST YOUR CODE ON A SAMPLE OF DATA TO FIND BUGS
        Dim lr As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Dim k, i As Long
        Dim V, S
        Dim store As Long
        V = Array("Louis", "Becky") ' add the words you want to delete and remember that this is a CASE SENSITIVE search
       
        For i = 1 To 10 '10 is the column J change it accordingly
                    For k = lr To 2 Step -1
                            For Each S In V
                              If InStr(1, Cells(k, i), S) > 0 Then
                                store = store + 1
                              End If
                            Next S
                                    If store >= 1 Then
                                        Cells(k, i).EntireRow.Delete
                                    End If
                                store = 0
                    Next k
            lr = Range("A" & Rows.Count).End(xlUp).Row
        Next i
End Sub
Thx - works excellent.
 
Upvote 0
How can I add more lines when all fields are used in this string: V = Array("Louis", "Becky") ... ... ...
I tried to add another string below to the first one V = Array("Louis2", "Becky2") - but that is not working. Code is not crashing either but it only runs trough the first one.
 
Upvote 0
Try this
VBA Code:
Sub Row_Deleter_2()
        '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _
        ALWAYS TEST YOUR CODE ON A SAMPLE OF DATA TO FIND BUGS
        Dim lr As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Dim k, i As Long
        Dim V, S
        Dim store As Long
        V = Array("Louis", "Becky") ' add the words you want to delete and remember that this is a CASE SENSITIVE search
       
        For i = 1 To 10 '10 is the column J change it accordingly
                    For k = lr To 2 Step -1
                            For Each S In V
                              If InStr(1, Cells(k, i), S) > 0 Then
                                store = store + 1
                              End If
                            Next S
                                    If store >= 1 Then
                                        Cells(k, i).EntireRow.Delete
                                    End If
                                store = 0
                    Next k
            lr = Range("A" & Rows.Count).End(xlUp).Row
        Next i
End Sub
Can add another line V = Array("Louis", "Becky") etc. below to exisiting one?
 
Upvote 0

Forum statistics

Threads
1,215,106
Messages
6,123,124
Members
449,097
Latest member
mlckr

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