Deleting Rows with certain text strings

viraim

New Member
Joined
Mar 9, 2009
Messages
4
Hi,

Sorry if this is not an appropriate post, but I know nothing about Excel VBA and macros, :confused: , but wish to automate the following task, and am hoping someone can pass me some VBA code that would work:

I wish to delete certain rows in my spreadsheet that do not include each of three seperate text strings (we can call them tstring1, tstring2 & tstring3).

Thanks in advance for your help.;)

Vince Raimondi
Manager of Marketing Sciences
The Marketing Workshop, Inc.
Norcross, GA
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Here's a quick and dirty version of what I think you're asking.

Change the tstring1, 2 & 3 values (lines 11,12,13) to whatever you need.

The delete method is commented out (last line). You can test it with the color change "oUnion.Interior.ColorIndex = 3" before you buy it.

Hope it helps.

Gary


Code:
Dim oCell As Range
Dim lRows As Long
Dim lCols As Long
Dim lRowCount As Long
Dim lColCount As Long
Dim iFlags As Integer
Dim oUnion As Range
Dim tstring1 As String
Dim tstring2 As String
Dim tstring3 As String
 
tstring1 = "Text 1 to search for"
tstring2 = "Text 2 to search for"
tstring3 = "Text 3 to search for"
 
lRows = ActiveSheet.UsedRange.Rows.Count
lCols = ActiveSheet.UsedRange.Columns.Count
 
For lRowCount = 1 To lRows
    For lColCount = 1 To lCols
    
        If InStr(1, UCase(ActiveSheet.Cells(lRowCount, lColCount)), UCase(tstring1)) Then
            iFlags = iFlags Or 1
        End If
        
        If InStr(1, UCase(ActiveSheet.Cells(lRowCount, lColCount)), UCase(tstring2)) Then
            iFlags = iFlags Or 2
        End If
        
        If InStr(1, UCase(ActiveSheet.Cells(lRowCount, lColCount)), UCase(tstring3)) Then
            iFlags = iFlags Or 4
        End If
    Next lColCount
    
    If iFlags <> 7 Then
        If oUnion Is Nothing Then
            Set oUnion = ActiveSheet.Rows(lRowCount)
        Else
            Set oUnion = Union(oUnion, ActiveSheet.Rows(lRowCount))
        End If
    End If
    
    iFlags = 0
    
Next lRowCount
 
oUnion.Interior.ColorIndex = 3
'oUnion.Rows.Delete
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

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