If No Value

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
I have the following code to delete a row if the value in column C is Monday. My question is, if there is no Monday value will there be an issue, so far I have run the code and I do not see any problems. Is there an alternative.

Thank You

Code

Sub DeleteMonday()




Dim lr As Long
Dim w As Long

Sheets("Sheet1").Activate

lr = Range("C" & Rows.Count).End(xlUp).Row


For w = 2 To lr

If Cells(w, "C").Value = "Monday" Then

Cells(w, "C").EntireRow.Delete

End If

Next w

End Sub
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
When deleting rows you should run the loop backwards
If no "Monday" is found you should have no problems
See code marked in red for changes made to your script.
Code:
Sub DeleteMonday()
Application.ScreenUpdating = False
Dim lr As Long
Dim w As Long
Sheets("Sheet1").Activate
lr = Range("C" & Rows.Count).End(xlUp).Row

    [COLOR=#ff0000]For w = lr To 2 Step -1[/COLOR]
        If Cells(w, "C").Value = "Monday" Then
            Cells(w, "C").EntireRow.Delete
        End If
    Next w
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Is there an alternative.
Yes, here's one
Code:
Sub I_Dont_Like_Monday()
   With Sheets("Sheet1").Columns(3)
      .Replace "Monday", "#N/A", xlWhole, , False, , False
      On Error Resume Next
      .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
      On Error GoTo 0
   End With
End Sub
 
Last edited:
Upvote 0
Here is a way to do it with filter which may be faster.

Code:
Sub Filter_Monday()
Application.ScreenUpdating = False
Dim ans As String
On Error GoTo M
ans = "Monday"
    
    With ActiveSheet.Range(Cells(1, 3), Cells(Cells(Rows.Count, 3).End(xlUp).Row, 3))
        .AutoFilter Field:=1, Criteria1:=ans
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With
    
Exit Sub
M:
MsgBox "No Rows with your criteria found"
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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