VB - How do I delete rows based on a cell values within those rows?

Jed Shields

Active Member
Joined
Sep 7, 2011
Messages
283
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I'm trying to delete rows that have a specific name in column CL. There are currenly two names and I have found and modified a piece of code but it doesn't work. Any pointers to what I've done wrong? I've rem'd out the code that does work when it looks for just the one name.

Code:
Sub DeleteRows()
  
    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 ActiveSheet

       
        .Select

       
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        
        .DisplayPageBreaks = False

        
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        
        For Lrow = Lastrow To Firstrow Step -1

        
            With .Cells(Lrow, "CL")

                If Not IsError(.Value) Then


                    'If .Value = "Veenod Kurup" Then .EntireRow.Delete   'this works fine when only looking for one name
                    
                    If .Cells(Lrow, "CL").Value = "Veenod Kurup" Or _
                    .Cells(Lrow, "CL").Value = "Robert Pitt" _
                    Then .Rows(Lrow).Delete

                End If
            End With
        Next Lrow
    End With

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

Cheers,
Jed
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You are already within a "With .Cells..." block, so you do not want "Cells" in your code.
Try mirroring the line of code that you commented out, something like this:
Code:
                    If .Value = "Veenod Kurup" Or .Value = "Robert Pitt" Then .EntireRow.Delete
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,685
Members
449,117
Latest member
Aaagu

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