Advertisement
sathana

Comparing dates of scans

Dec 7th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. Sub FindEarliestAndLatestDatesWithColor()
  2. Dim ws As Worksheet
  3. Dim lastRow As Long
  4. Dim i As Long
  5. Dim earliestDate As Date
  6. Dim latestDate As Date
  7. Dim dateFound As Boolean
  8.  
  9. ' Set the worksheet (change "Sheet1" to your actual sheet name)
  10. Set ws = ThisWorkbook.Sheets("Data(10-30-23)")
  11.  
  12. ' Find the last row with data in column A
  13. lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
  14.  
  15. ' Loop through each row
  16. For i = 2 To lastRow ' Assuming data starts from row 2
  17.  
  18. ' Initialize earliestDate, latestDate, and dateFound for each row
  19. earliestDate = DateValue("12/31/9999")
  20. latestDate = DateValue("1/1/1900")
  21. dateFound = False
  22.  
  23. ' Array of columns to check
  24. Dim columnsToCheck As Variant
  25. columnsToCheck = Array("AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AP", "AS", "AZ", "AW", "BB")
  26.  
  27. ' Loop through each column in the array
  28. For Each col In columnsToCheck
  29. ' Check if the cell in the current row and column is a date
  30. If IsDate(ws.Cells(i, col).Value) Then
  31. ' Update earliestDate if the current date is earlier
  32. If ws.Cells(i, col).Value < earliestDate Then
  33. earliestDate = ws.Cells(i, col).Value
  34. End If
  35. ' Update latestDate if the current date is later
  36. If ws.Cells(i, col).Value > latestDate Then
  37. latestDate = ws.Cells(i, col).Value
  38. End If
  39. ' Set dateFound to true
  40. dateFound = True
  41. End If
  42. Next col
  43.  
  44. ' Check if any dates were found
  45. If dateFound Then
  46. ' Input the earliest and latest dates to columns EL and EM in the current row
  47. ws.Cells(i, "EL").Value = Format(earliestDate, "mm/dd/yyyy")
  48. ws.Cells(i, "EL").NumberFormat = "mm/dd/yyyy" ' Set the date format explicitly
  49.  
  50. ws.Cells(i, "EM").Value = Format(latestDate, "mm/dd/yyyy")
  51. ws.Cells(i, "EM").NumberFormat = "mm/dd/yyyy" ' Set the date format explicitly
  52.  
  53.  
  54.  
  55. ' Check if there is a date in column EK and if it is before the date in column EM
  56. If IsDate(ws.Cells(i, "EK").Value) And ws.Cells(i, "EK").Value < latestDate Then
  57. ' Highlight the entire row in light red
  58. ws.Rows(i).Interior.Color = RGB(255, 200, 200)
  59. End If
  60. Else
  61. ' No dates found, color cells in blue
  62. ws.Cells(i, "EL").Interior.Color = RGB(173, 216, 230) ' Light blue color
  63. ws.Cells(i, "EM").Interior.Color = RGB(173, 216, 230) ' Light blue color
  64.  
  65. End If
  66. Next i
  67. End Sub
  68.  
  69.  
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement