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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,215,583
Messages
6,125,661
Members
449,247
Latest member
wingedshoes

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