loop to delete row

rebaS

New Member
Joined
Jun 26, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm trying to delete rows that have "Yes" in column B. Please help me understand why this isn't working. Thank you!

VBA Code:
Sub nocompound()

    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i, comp
    
Set ws = ThisWorkbook.Worksheets("rawday") 'sheet's name
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row 'last row in column B
        
    For i = lastRow To 2 Step -1
       comp = Cells(i, "B")
        If Not Empty Then 'Check if cell contains a valid date
            If comp = Yes Then
               ws.Cells(i, "B").EntireRow.Delete
               If Empty Then
                End If
            End If
        End If
    Next i
    
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this:
VBA Code:
Sub nocompound()

    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i as Long
    
    Set ws = ThisWorkbook.Worksheets("rawday") 'sheet's name
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row 'last row in column B
        
    For i = lastRow To 2 Step -1
        If ws.Cells(i, "B") = "Yes" Then ws.Rows(i).Delete
    Next i
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,222
Messages
6,123,709
Members
449,118
Latest member
MichealRed

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