Deleting row if value exists

thomassharp

Board Regular
Joined
Dec 10, 2014
Messages
84
Hi everyone, can the code below be modified to search for multiple values? So entering a list of 50 values to delete all rows containing each value?

Code:
Sub Loop_Example()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long


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


    With Sheet1


        .Select


        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView


        .DisplayPageBreaks = False


        Firstrow = 2
        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row


        For Lrow = Lastrow To Firstrow Step -1


            With .Cells(Lrow, "I")


                If Not IsError(.Value) Then


                    If .Value = "James Fleming & Co Ltd" Then .EntireRow.Delete
                   


                End If


            End With


        Next Lrow


    End With


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


End Sub

Thank you!!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi,

I think it a mistake to hard code that amount of text strings, I would store them in a worksheet range like this. My checkrange is a1:a10 on sheet 2 but you can put it anywhere


Code:
Sub Loop_Example()
Dim ChkRange As Range
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
Set ChkRange = Sheets("Sheet2").Range("A1:A10")
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    With Sheet1
        .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False
        Firstrow = 2
        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For Lrow = Lastrow To Firstrow Step -1
            With .Cells(Lrow, "I")
                If Not IsError(.Value) Then
                    If Not IsError(Application.Match(.Value, ChkRange, 0)) Then
                    .EntireRow.Delete
                    End If
                End If
            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,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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