Excel VBA - Compare Date and do the following if greater than

MHamid

Active Member
Joined
Jan 31, 2013
Messages
472
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I have the below code that is not doing what I need. It is clearing theadjacent data even if the date is less than the look-up date.

I need the code to find all "Completed" statuses in column C, thencompare the date in column D. If the date is greater than the date in cellT1, then clear the data from cells D thru F in that row and replacethe word "Completed" with "In progress".

Code:
'Add prior month date, then compare to Publication Date, and Clear data for all "Completed" in columns D, E & F
Dim ddate As Date
Dim rCell As Range
Dim r As Long
    If IsDate(Range("T1")) Then
        ddate = Range("T1").Value
    Else
        MsgBox "Non valid date"
        Exit Sub
    End If
    With Sheets("Report1")
      For Each rCell In .Range(.Cells(1, "D"), .Cells(.Rows.Count, "D").End(xlUp))
        If IsDate(rCell) Then
           If rCell >= ddate Then
                Application.ScreenUpdating = False
                lr = Cells(Rows.Count, "G").End(xlUp).Row
                    For r = 1 To lr
                        If Cells(r, "C") = "Completed" Then
                            Range(Cells(r, "D"), Cells(r, "F")).ClearContents
                            Selection.Replace What:="Completed", Replacement:="In Progress", LookAt:= _
                                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                                ReplaceFormat:=False
                        End If
                    Next r
                Application.ScreenUpdating = True
            End If
        End If
      Next rCell
    End With


What am I doing wrong?

Thank you
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try
Code:
   With Sheets("Report1")
      For Each rCell In .Range(.Cells(1, "D"), .Cells(.Rows.Count, "D").End(xlUp))
         If IsDate(rCell) Then
            If rCell >= ddate Then
               If rCell.Offset(, -1).Value = "Completed" Then
                  rCell.Offset(, -1).Value = "InProgress"
                  rCell.Resize(, 3).ClearContents
               End If
            End If
         End If
      Next rCell
   End With
 
Upvote 0
Glad to 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