Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub FindEarliestAndLatestDatesWithColor()
- Dim ws As Worksheet
- Dim lastRow As Long
- Dim i As Long
- Dim earliestDate As Date
- Dim latestDate As Date
- Dim dateFound As Boolean
- ' Set the worksheet (change "Sheet1" to your actual sheet name)
- Set ws = ThisWorkbook.Sheets("Data(10-30-23)")
- ' Find the last row with data in column A
- lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
- ' Loop through each row
- For i = 2 To lastRow ' Assuming data starts from row 2
- ' Initialize earliestDate, latestDate, and dateFound for each row
- earliestDate = DateValue("12/31/9999")
- latestDate = DateValue("1/1/1900")
- dateFound = False
- ' Array of columns to check
- Dim columnsToCheck As Variant
- columnsToCheck = Array("AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AP", "AS", "AZ", "AW", "BB")
- ' Loop through each column in the array
- For Each col In columnsToCheck
- ' Check if the cell in the current row and column is a date
- If IsDate(ws.Cells(i, col).Value) Then
- ' Update earliestDate if the current date is earlier
- If ws.Cells(i, col).Value < earliestDate Then
- earliestDate = ws.Cells(i, col).Value
- End If
- ' Update latestDate if the current date is later
- If ws.Cells(i, col).Value > latestDate Then
- latestDate = ws.Cells(i, col).Value
- End If
- ' Set dateFound to true
- dateFound = True
- End If
- Next col
- ' Check if any dates were found
- If dateFound Then
- ' Input the earliest and latest dates to columns EL and EM in the current row
- ws.Cells(i, "EL").Value = Format(earliestDate, "mm/dd/yyyy")
- ws.Cells(i, "EL").NumberFormat = "mm/dd/yyyy" ' Set the date format explicitly
- ws.Cells(i, "EM").Value = Format(latestDate, "mm/dd/yyyy")
- ws.Cells(i, "EM").NumberFormat = "mm/dd/yyyy" ' Set the date format explicitly
- ' Check if there is a date in column EK and if it is before the date in column EM
- If IsDate(ws.Cells(i, "EK").Value) And ws.Cells(i, "EK").Value < latestDate Then
- ' Highlight the entire row in light red
- ws.Rows(i).Interior.Color = RGB(255, 200, 200)
- End If
- Else
- ' No dates found, color cells in blue
- ws.Cells(i, "EL").Interior.Color = RGB(173, 216, 230) ' Light blue color
- ws.Cells(i, "EM").Interior.Color = RGB(173, 216, 230) ' Light blue color
- End If
- Next i
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement