Need to highlight cell with date

MacroBB

New Member
Joined
Mar 20, 2023
Messages
12
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello all,

Can I get some help on how to use VBA to highlight cells in a daily report that have approximately 500 lines with multiple date columns? Only need to high light cells in Column B if it's equal to or later/bigger than Column A but earlier/smaller than any of the following column C to F? For example:
Date ADate B (Column need to be highlighted)Date CDate DDate EDate F
Jan 10Jan 9Jan 9
Jan 10Jan 11Jan 12Jan 12Jan 13
Jan 15Jan 15Jan 9Jan 15Jan 13
Jan 15Jan 16Jan 15Jan 16Jan 17
Feb 1Jan 31Feb 1Feb 1Feb 1

What I would like to see:
Date ADate B (Column need to be highlighted)Date CDate DDate EDate F
Jan 10Jan 9Jan 9
Jan 10Jan 11Jan 12Jan 12Jan 13
Jan 15Jan 15Jan 9Jan 15Jan 13
Jan 15Jan 16Jan 15Jan 16
Feb 1Jan 31Feb 1Feb 1Feb 1

Thanks for your help!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
As for code, this seems to work if the code is on the sheet you want to use it on and your columns are as shown. I elected to coerce date values to double data type in case your dates were strings. Not sure if it would have mattered.
VBA Code:
Sub testRangeArray()
Dim ary As Variant
Dim Lrow As Long
Dim i As Integer, n As Integer, dblDate1 As Double, dblDate2 As Double
Dim blnGreater As Boolean

Lrow = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To Lrow
    blnGreater = False
    dblDate1 = Range("A" & i)
    dblDate2 = Range("A" & i).Offset(0, 1)
    If dblDate1 < dblDate2 Then
        ary = Range("C" & i & ":F" & i)
        For n = 1 To UBound(ary, 2)
            If dblDate2 < CDbl(ary(1, n)) And WorksheetFunction.IsNumber(CDbl(ary(1, n))) Then
                blnGreater = True
                 Exit For
            End If
        Next
    End If
    If blnGreater = True Then Range("A" & i).Offset(0, 1).Font.Color = vbRed
Next

End Sub
Result:
1679363200026.png
 
Upvote 0
As for code, this seems to work if the code is on the sheet you want to use it on and your columns are as shown. I elected to coerce date values to double data type in case your dates were strings. Not sure if it would have mattered.
VBA Code:
Sub testRangeArray()
Dim ary As Variant
Dim Lrow As Long
Dim i As Integer, n As Integer, dblDate1 As Double, dblDate2 As Double
Dim blnGreater As Boolean

Lrow = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To Lrow
    blnGreater = False
    dblDate1 = Range("A" & i)
    dblDate2 = Range("A" & i).Offset(0, 1)
    If dblDate1 < dblDate2 Then
        ary = Range("C" & i & ":F" & i)
        For n = 1 To UBound(ary, 2)
            If dblDate2 < CDbl(ary(1, n)) And WorksheetFunction.IsNumber(CDbl(ary(1, n))) Then
                blnGreater = True
                 Exit For
            End If
        Next
    End If
    If blnGreater = True Then Range("A" & i).Offset(0, 1).Font.Color = vbRed
Next

End Sub
Result:
View attachment 87965
 
Upvote 0
As for code, this seems to work if the code is on the sheet you want to use it on and your columns are as shown. I elected to coerce date values to double data type in case your dates were strings. Not sure if it would have mattered.
VBA Code:
Sub testRangeArray()
Dim ary As Variant
Dim Lrow As Long
Dim i As Integer, n As Integer, dblDate1 As Double, dblDate2 As Double
Dim blnGreater As Boolean

Lrow = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To Lrow
    blnGreater = False
    dblDate1 = Range("A" & i)
    dblDate2 = Range("A" & i).Offset(0, 1)
    If dblDate1 < dblDate2 Then
        ary = Range("C" & i & ":F" & i)
        For n = 1 To UBound(ary, 2)
            If dblDate2 < CDbl(ary(1, n)) And WorksheetFunction.IsNumber(CDbl(ary(1, n))) Then
                blnGreater = True
                 Exit For
            End If
        Next
    End If
    If blnGreater = True Then Range("A" & i).Offset(0, 1).Font.Color = vbRed
Next

End Sub
Result:
View attachment 87965

Thank you for your help, forgot to mention there are headers for each column and seems like the codes did not work when headers are present.

1679413990851.png
 
Upvote 0
try changing For i = 1
to
For i = 2
if the header is just one row. If not, then adjust according to the number of used rows for header.
 
Upvote 1
try changing For i = 1
to
For i = 2
if the header is just one row. If not, then adjust according to the number of used rows for header.
It worked with the new edit, thank you!!
Just one more question. What if the condition changed to highlighting cells in Column B if it's equal to or later/bigger than Column A but equal to or smaller than any of the following column C to F?
 
Upvote 0
You're welcome, and thanks for the recognition.
 
Upvote 0
@Micron
If I may...
What if the situation changed and I want to have two highlight colors? Say one for equal to and another color for smaller?
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

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