i 've been trying to make a macro to highlight dates with their entire rows if they fell into a specific date interval. The problem i have encoutered is : when macro finds a certain date it colours the entire row of that date and then should go onto the next .find with .findnext. However the macro gets stuck in a loop in here
Do While Not c Is Nothing
c.EntireRow.Interior.Color = vbCyan
Set c = Dates.FindNext
Loop
with c value as 2021.03.01 (as StartDate) My code looks like this:
Private Sub CommandButton2_Click()
Dim c As Range
Dim first As String
Dim last As String
Dim StartDate As Date
Dim EndDate As Date
Dim DateLooper As Date
first = CLng(Range("E2").Value)
last = CLng(Range("G2").Value)
For Each Cell In ActiveSheet.Range("H2:H" & Cells(Rows.Count, 8).End(xlUp).Row)
Sheet = Cell
StartDate = first
EndDate = last
For DateLooper = StartDate To EndDate
Set Dates = Worksheets(Sheet).Range("P:P")
Set c = Dates.Find(What:=DateLooper)
Do While Not c Is Nothing
c.EntireRow.Interior.Color = vbCyan
Set c = Dates.FindNext(c)
Loop
Next DateLooper
Set c = Nothing
Next Cell
End Sub
What's the problem here? Thank you for your time and help. Maybe it's because of the c being a date?
Do
loop will never end if c is not nothing. The usual way to exit is to store the address of the first found cell and then loop until you get back to that address meaning that you are back to where you started. – SJR