Trying to delete rows that do not contain specific text

MPH88

New Member
Joined
Sep 28, 2017
Messages
13
Hiya. I am having fun learning this vba stuff! But, I'm stuck.

My Book contains businesses and address info, like this:
<style>table { }tr { }col { }br { }td { padding-top: 1px; padding-right: 1px; padding-left: 1px; color: black; font-size: 12pt; font-weight: 400; font-style: normal; text-decoration: none; font-family: Calibri, sans-serif; vertical-align: bottom; border: medium none; white-space: nowrap; }.xl65 { color: white; font-weight: 700; border-color: rgb(142, 169, 219) currentcolor rgb(142, 169, 219) rgb(142, 169, 219); border-style: solid none solid solid; border-width: 0.5pt medium 0.5pt 0.5pt; background: rgb(68, 114, 196) none repeat scroll 0% 0%; }.xl66 { color: white; font-weight: 700; border-color: rgb(142, 169, 219) currentcolor; border-style: solid none; border-width: 0.5pt medium; background: rgb(68, 114, 196) none repeat scroll 0% 0%; }.xl67 { color: white; font-weight: 700; border-color: rgb(142, 169, 219) rgb(142, 169, 219) rgb(142, 169, 219) currentcolor; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt medium; background: rgb(68, 114, 196) none repeat scroll 0% 0%; }.xl68 { border-color: rgb(142, 169, 219) currentcolor rgb(142, 169, 219) rgb(142, 169, 219); border-style: solid none solid solid; border-width: 0.5pt medium 0.5pt 0.5pt; background: rgb(217, 225, 242) none repeat scroll 0% 0%; }.xl69 { border-color: rgb(142, 169, 219) currentcolor; border-style: solid none; border-width: 0.5pt medium; background: rgb(217, 225, 242) none repeat scroll 0% 0%; }.xl70 { border-color: rgb(142, 169, 219) currentcolor; border-style: solid none; border-width: 0.5pt medium; background: rgb(217, 225, 242) none repeat scroll 0% 0%; }.xl71 { border-color: rgb(142, 169, 219) rgb(142, 169, 219) rgb(142, 169, 219) currentcolor; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt medium; background: rgb(217, 225, 242) none repeat scroll 0% 0%; }.xl72 { border-color: rgb(142, 169, 219) currentcolor rgb(142, 169, 219) rgb(142, 169, 219); border-style: solid none solid solid; border-width: 0.5pt medium 0.5pt 0.5pt; }.xl73 { border-color: rgb(142, 169, 219) currentcolor; border-style: solid none; border-width: 0.5pt medium; }.xl74 { border-color: rgb(142, 169, 219) currentcolor; border-style: solid none; border-width: 0.5pt medium; }.xl75 { border-color: rgb(142, 169, 219) rgb(142, 169, 219) rgb(142, 169, 219) currentcolor; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt medium; }.xl76 { border-color: rgb(142, 169, 219) rgb(142, 169, 219) rgb(142, 169, 219) currentcolor; border-style: solid solid solid none; border-width: 0.5pt 0.5pt 0.5pt medium; background: rgb(217, 225, 242) none repeat scroll 0% 0%; }.xl77 { text-align: center; }</style>
ABCDEFGHIJKLMN
1BureauNumberPrimary NameStreet AddressCityZip CodeStateCountyGoverning ClassDescriptionExModUpdated OnExModYearInsurerNameCoverageDate
23484M P A SALES, INC.13700 VAN NESS AVENUEGARDENA90249CALos Angeles County8018STORES-WHOLESALE852/16/1817Employers Assurance Company##########
34095C MONDAVI & FAMILYP. O. BOX 191 2800 NORTH MAIN STREETST. HELENA94574CANapa County2142WINERIES1332/16/1817Travelers Property Casualty Company of America##########
47028(DBA) BAY AREA BAGELS260 LORTON AVEBURLINGAME94010CASan Mateo County8078SANDWICH SHOPS772/16/1818Mid-Century Insurance Company##########
59770GREEN VALLEY LANDSCAPE AND MAINTENANCE INC1182 INDUSTRIAL AVEESCONDIDO92029CASan Diego County42LANDSCAPE GARDENING1252/16/1818

<tbody>
</tbody>

My goal is to find the businesses within certain counties (Column G). I want to delete the data that is irrelevant.

I poked around online and found a walk-through that looked promising. I re-purposed the code (pasted below).

Goal: I want to delete the rows that do not contain multiple specific text values in column G. Here is the code:

Code:
Sub RemoveCounties()
'
' RemoveCounties Macro
'

'
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim Counties As Variant
    
    Counties = Array("Los Angeles County", "Orange County", "Riverside County", "San Bernardino County", "San Diego County")

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet

        'We select the sheet so we can change the window view
        .Select

        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False

        'Set the first and last row to loop through
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1

            'We check the values in the G column in this example
            With .Cells(Lrow, "G")

                Select Case .Value
                [B]Case Is <> Counties[/B]: .EntireRow.Delete
                End Select

                'If Not IsError(.Value) Then

                    'If .Value <> Counties Then .EntireRow.Delete
                    'This will delete each row that does not contain one of the counties listed in the variable above
                    'in Column G, case sensitive.

                    'End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With


End Sub

I get a "Type mismatch" error and the debugger points to this line "Case Is <> Counties"

FYI...The walk-through outlined different options including "If Not IsError(.Value) Then" and also "Select Case .Value". I got the same error on both approaches.

Any suggestions?

Questions: Does it matter if my data is set as a table versus an array?

Thanks a bunch.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
It's coming along. Trial...
Code:
 Dim Counties () As Variant
Counties(Lrow - 1)
Your'e using an array with positions 0 to lastrow - 1.. You don't need to manually enter the names in an array. You may want to decide do you really want to delete the whole row of just some data within it... there's a difference. HTH. Dave
 
Upvote 0
Hoping to get a little more help on this one. Again, I want to keep all the records from certain counties ("Los Angeles County", "Orange County", "Riverside County", "San Bernardino County", "San Diego County"). And, I want to delete all the other irrelevant records.

I came across two approaches that are designed to find certain text and delete the rows that do not contain it (the opposite of what I want). So, I tried swapping out the = for <>. I have tried:
Case Is <> Counties: .EntireRow.Delete

I get a "Type mismatch" error here. I realize I am trying to get "Case" to look at my variable "Counties" and find stuff that doesn't fit. Is this possible through this approach? Is there a way to use
Code:
Case Is Not =
or
Code:
Case Not Is =
?

Or, can I remove my variable and use
Code:
Case = "Los Angeles County", "Orange County", "Riverside County", "San Bernardino County", "San Diego County"

The other approach is:

If .Value <> Counties Then .EntireRow.Delete

I think I understand why this doesn't work. From what I read this approach will look for a single value...not multiple. So, instead of my "Counties" variable it wants just one string. Correct?

How would you do it?
 
Upvote 0
Try
Code:
        For Lrow = Lastrow To Firstrow [COLOR=#ff0000]+ 1[/COLOR] Step -1

            'We check the values in the G column in this example
            With .Cells(Lrow, "G")
               If Not UBound(Filter(Counties, .Value, True, vbTextCompare)) = 0 Then .EntireRow.Delete
            End With

        Next Lrow
Note the +1 in red, without that you'll delete the header row.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Anyone who is working on the same problem...here's the final code that did the job:

Code:
Sub RemoveCounties()
'
' RemoveCounties Macro
'

'
    Dim FirstRow As Long
    Dim LastRow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim Counties As Variant   ' Change this variable name to suit.

' Make sure the also change the associated array as well as the the column
    
    Counties = Array("Los Angeles County", "Orange County", "Riverside County", "San Bernardino County", "San Diego County")

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet

        'We select the sheet so we can change the window view
        .Select

        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False

        'Set the first and last row to loop through
        FirstRow = .UsedRange.Cells(1).Row
        LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        'We loop from Lastrow to Firstrow (bottom to top). The + 1 makes sure you do not delete your header row.
        For Lrow = LastRow To FirstRow + 1 Step -1

            'We check the values in the G column in this example
            With .Cells(Lrow, "G")
               If Not UBound(filter(Counties, .Value, True, vbTextCompare)) = 0 Then .EntireRow.Delete
            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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