VBA - Find text and Delete Entire Row in Multiple Sheet

billylui9

New Member
Joined
Feb 2, 2021
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
Please HELP write a VBA to finding a Text in Multiple Sheet and delete the entire row if the text is found.

This String will appear many time,

I.e. if the string "BONNIE" is present in any cell in any string position in any Sheet the entire row will be deleted.

URGENT, Many thanks
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Is "BONNIE" in any particular column ?
Is it the only text in the cell OR is it part of a string ?
Are there sheets that are not to be included in the search ?
 
Upvote 0
This is one of many methods :

VBA Code:
Option Explicit

Sub FindInLists()
     '
     ' Select FindInLists Macro
     '
    
    Dim SheetsToSearch, SrchStrg As String, ws As Excel.Worksheet, r As Range
    SheetsToSearch = Array("Sheet1") '// Enter the exact sheet names of sheets to be searched
    
    SrchStrg = Application.InputBox("Enter Term to search ", "Search Term", Type:=2)
    
    For Each ws In ThisWorkbook.Sheets
        If Not IsError(Application.Match(ws.Name, SheetsToSearch, 0)) Then
            With ws.Range("A2:Z10000")  'EDIT RANGE AS REQUIRED
                Set r = .Find(what:=SrchStrg, After:=.Range("A1"))   'find the cell whose value is equal to SrchStrg and activate it
                If Not r Is Nothing Then
                    'ws.Activate: r.Activate
                    r.EntireRow.Delete
                    
                'PUT YOUR CODE HERE TO WRITE THE TICKER DATA SOMEWHERE FOR REVIEW
                    
                ElseIf r Is Nothing Then
                    MsgBox "Search term does not exist. ", vbInformation, "Item Not Found"
                End If
            End With
        End If
    Next
    
End Sub
 
Upvote 0
@Logit Maybe substitute ???
VBA Code:
With ws.Range("A2:Z10000")

FOR

VBA Code:
With ws.UsedRange
 
Upvote 0
Is "BONNIE" in any particular column ? YES, could be any cell in any sheet
Is it the only text in the cell OR is it part of a string ? Indiviual text or part of string
Are there sheets that are not to be included in the search ? Included
 
Upvote 0
I believe the solution provided by @Logit should do the trick !
 
Upvote 0
This is one of many methods :

VBA Code:
Option Explicit

Sub FindInLists()
     '
     ' Select FindInLists Macro
     '
   
    Dim SheetsToSearch, SrchStrg As String, ws As Excel.Worksheet, r As Range
    SheetsToSearch = Array("Sheet1") '// Enter the exact sheet names of sheets to be searched
   
    SrchStrg = Application.InputBox("Enter Term to search ", "Search Term", Type:=2)
   
    For Each ws In ThisWorkbook.Sheets
        If Not IsError(Application.Match(ws.Name, SheetsToSearch, 0)) Then
            With ws.Range("A2:Z10000")  'EDIT RANGE AS REQUIRED
                Set r = .Find(what:=SrchStrg, After:=.Range("A1"))   'find the cell whose value is equal to SrchStrg and activate it
                If Not r Is Nothing Then
                    'ws.Activate: r.Activate
                    r.EntireRow.Delete
                   
                'PUT YOUR CODE HERE TO WRITE THE TICKER DATA SOMEWHERE FOR REVIEW
                   
                ElseIf r Is Nothing Then
                    MsgBox "Search term does not exist. ", vbInformation, "Item Not Found"
                End If
            End With
        End If
    Next
   
End Sub

Thanks for Helping, but...

Only 1st Sheet 1st "Bonnie"'s row deleted, the following matched case not affect as attach.
 

Attachments

  • 1a.jpg
    1a.jpg
    50 KB · Views: 24
  • 2a.jpg
    2a.jpg
    50.4 KB · Views: 24
Upvote 0
Try using
VBA Code:
Option Compare Text
Sub MM1()
Dim ws As Worksheet, cell As Range
For Each ws In Worksheets
    With ws
        For Each cell In .UsedRange
            If InStr(cell, "Bonnie") Then
            .Rows(cell.Row).Delete
            End If
        Next cell
    End With
Next ws
End Sub
 
Upvote 0
Try using
VBA Code:
Option Compare Text
Sub MM1()
Dim ws As Worksheet, cell As Range
For Each ws In Worksheets
    With ws
        For Each cell In .UsedRange
            If InStr(cell, "Bonnie") Then
            .Rows(cell.Row).Delete
            End If
        Next cell
    End With
Next ws
End Sub

It work for my Sample Excel and delete text "Bonnie" in three different Sheet, but dosen't work on my working file and show :

"type mismatch (Error 13)"

I have no idea what happen ( Actaully, it work few time when I reduce no. of Sheet and Row of Content )
 

Attachments

  • DDD.jpg
    DDD.jpg
    59.6 KB · Views: 19
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,038
Members
449,205
Latest member
Eggy66

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