match color column

Urraco

Board Regular
Joined
Apr 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Hi,
a little problem...
columns A:D are filled with dates starting with 1992-01-01...
column E is filled with the same dates but distributed over one column
some data on column E is font colored in red
I want to add a "<" symbol to the dates in A:D that corresponds to the red color data in column E
i.e. if 1992-01-05 in column E is in red, then 1992-01-05 in columns A:D will become 1992-01-05<
how can this be done?
thanks

Book1.xlsx
ABCDE
11992-01-011992-01-021992-01-031992-01-041992-01-01
21992-01-051992-01-061992-01-071992-01-081992-01-02
31992-01-091992-01-101992-01-111992-01-121992-01-03
41992-01-131992-01-141992-01-151992-01-161992-01-04
51992-01-171992-01-181992-01-191992-01-201992-01-05
61992-01-211992-01-221992-01-231992-01-241992-01-06
71992-01-251992-01-261992-01-271992-01-281992-01-07
81992-01-291992-01-301992-01-311992-02-011992-01-08
91992-02-021992-02-031992-02-041992-02-051992-01-09
101992-02-061992-02-071992-02-081992-02-091992-01-10
111992-02-101992-02-111992-02-121992-02-131992-01-11
121992-02-141992-02-151992-02-161992-02-171992-01-12
131992-02-181992-02-191992-02-201992-02-211992-01-13
141992-02-221992-02-231992-02-241992-02-251992-01-14
151992-02-261992-02-271992-02-281992-02-291992-01-15
161992-03-011992-03-021992-03-031992-03-041992-01-16
171992-03-051992-03-061992-03-071992-03-081992-01-17
181992-03-091992-03-101992-03-111992-03-121992-01-18
191992-01-19
201992-01-20
211992-01-21
221992-01-22
231992-01-23
241992-01-24
251992-01-25
261992-01-26
271992-01-27
281992-01-28
291992-01-29
301992-01-30
311992-01-31
321992-02-01
Sheet4
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi,

This will work for you:
VBA Code:
Sub test()
  Dim columnE As Range, columnsAD As Variant, lRow As Long, i As Long, j As Long
  Dim redDates As Object
  Set redDates = CreateObject("Scripting.Dictionary")
  lRow = Cells(Rows.Count, "E").End(xlUp).Row
 
  Set columnE = Range("E1:E" & lRow)
  columnsAD = Range("A1:D" & lRow)
 
  For Each Rng In columnE
    If Not redDates.Exists(Rng.Value) And Rng.Font.Color = 255 Then
      redDates.Add Rng.Value, 1
    End If
  Next
 
  For i = 1 To UBound(columnsAD, 1)
    For j = 1 To UBound(columnsAD, 2)
      If redDates.Exists(columnsAD(i, j)) Then
        columnsAD(i, j) = Format(columnsAD(i, j), "yyyy-mm-dd") & "<"
      End If
    Next
  Next
  Range("A1").Resize(UBound(columnsAD, 1), UBound(columnsAD, 2)).Value = columnsAD
End Sub
 
Upvote 0
Solution
If you only have one instance of each date to change in columns A:D then the below find loop will work:
VBA Code:
Sub test()
    Dim lkupRng As Range, MasterRng As Range, rCell As Range, fCell As Range
    
    Set lkupRng = Range("A1:D" & Range("A" & Rows.Count).End(xlUp).Row)
    Set MasterRng = Range("E1:E" & Range("E" & Rows.Count).End(xlUp).Row)
    
    For Each rCell In MasterRng
        If rCell.Font.Color = vbRed Then
            Set fCell = lkupRng.Find(rCell.Value, , , xlWhole)
            If Not fCell Is Nothing Then
                fCell = fCell & "<"
            End If
            Set fCell = Nothing
        End If
    Next rCell
End Sub
 
Upvote 0
Hi @Georgiboy
If I were you I would avoid two issues:
1. Set lkupRng = Range("A1:D" & Range("A" & Rows.Count).End(xlUp).Row) Not all of the columns might be in the same length.
Column E would be the safest length since it includes all the values.
2. fCell = fCell & "<" this may reset the formatting to the default date format.
 
Upvote 0
Hi @Flashbond
If I were you I would make sure all of your variables are defined.
Rng has not been defined so the code returns an error if 'Opton Explicit' is used.
 
Upvote 0
very good code(y)!
Thank you!


Hi,

This will work for you:
VBA Code:
Sub test()
  Dim columnE As Range, columnsAD As Variant, lRow As Long, i As Long, j As Long
  Dim redDates As Object
  Set redDates = CreateObject("Scripting.Dictionary")
  lRow = Cells(Rows.Count, "E").End(xlUp).Row
 
  Set columnE = Range("E1:E" & lRow)
  columnsAD = Range("A1:D" & lRow)
 
  For Each Rng In columnE
    If Not redDates.Exists(Rng.Value) And Rng.Font.Color = 255 Then
      redDates.Add Rng.Value, 1
    End If
  Next
 
  For i = 1 To UBound(columnsAD, 1)
    For j = 1 To UBound(columnsAD, 2)
      If redDates.Exists(columnsAD(i, j)) Then
        columnsAD(i, j) = Format(columnsAD(i, j), "yyyy-mm-dd") & "<"
      End If
    Next
  Next
  Range("A1").Resize(UBound(columnsAD, 1), UBound(columnsAD, 2)).Value = columnsAD
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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