Column color fill

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a table (see table below) and I have a row that has 5 dates in 5 consecutive columns. There is a blank row between each set of data. I have five months of columns, each month a block of five columns. I would like to have a macro that would start at column G and check to see if Date 1 fits the case for that column (30 days divided by 5 = 6, so Case 1 = Date <6). If date 1 fits case 1, then add the date to the cell and change to color based on the color matrix. If the date in the date 1 column does not fit the case for column G, move one cell to the right and see if date 1 fits into that date range. and loop through the remaining cells until it reaches the end of the last month. When Date 1 is done, loop through the cells for Date 2, etc, through Date 5. Then I need it to skip the blank row and repeat the process for each row with data in it.

I have included a set of date numbers that correspond to the month in the top row, located in the cells below the table. I was thinking that this might be helpful in solving the date checking component. I have worked out a way to do this, but I use a large If then statement to do just one month for one date. Doing it this way would be very slow and tedious as well as take up a lot of code. I know there has to be a more elegant way to write the code than what I have.


Here is what I have started with. NOTE: The cell references are off because I have created a more simplified table the my original.
My VBA Code:
VBA Code:
Dim VATDate As Date

VATDate = CDate(Range("J" & ActiveCell.Row))

If Month(VATDate) = Range("BG3").Value Then

    If Day(VATDate) <= 6 Then
    
        Range("R" & ActiveCell.Row).Interior.Color = RGB(255, 230, 150)
        Range("R" & ActiveCell.Row).Value = Range("J" & ActiveCell.Row).Value
        Range("S" & ActiveCell.Row).Select
        
    ElseIf Day(VATDate) > 6 And Day(VATDate) <= 12 Then
    
        Range("S" & ActiveCell.Row).Interior.Color = RGB(255, 230, 150)
        Range("S" & ActiveCell.Row).Value = Range("J" & ActiveCell.Row).Value
        Range("T" & ActiveCell.Row).Select
    
    ElseIf Day(VATDate) > 12 And Day(VATDate) <= 18 Then
    
        Range("T" & ActiveCell.Row).Interior.Color = RGB(255, 230, 150)
        Range("T" & ActiveCell.Row).Value = Range("J" & ActiveCell.Row).Value
        Range("U" & ActiveCell.Row).Select
    
    ElseIf Day(VATDate) > 18 And Day(VATDate) <= 24 Then
    
        Range("U" & ActiveCell.Row).Interior.Color = RGB(255, 230, 150)
        Range("U" & ActiveCell.Row).Value = Range("J" & ActiveCell.Row).Value
        Range("V" & ActiveCell.Row).Select
    
    ElseIf Day(VATDate) > 24 Then
    
        Range("V" & ActiveCell.Row).Interior.Color = RGB(255, 230, 150)
        Range("V" & ActiveCell.Row).Value = Range("J" & ActiveCell.Row).Value
        Range("W" & ActiveCell.Row).Select
    
    End If

End If

Data Table:

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1Date 1Date 2Date 3Date 4Date 5Display DataMayJuneJulyAugustSeptember
2
31-Jun-202019-Jun-202026-Jun-20202-May-20201-Jul-2020
4
513-Jul-202025-Jul-20204-Aug-202013-Aug-202020-Aug-2020
6
71-Aug-202015-Aug-202022-Aug-20206-Sep-202011-Sep-2020
8
9
10
11
12Case 1Case 2Case 3Case 4Case 5
13Case 1Date < 6
14Case 2Date > 6 AND Date <= 12ColorRGB
15Case 3Date > 12 AND Date <= 18Date 1255250150
16Case 4Date > 18 AND Date <= 24Date 2215240200
17Case 5Date > 24Date 3255215215
18Date 4200215240
19Date 5255220255
20
21Month Numbers56789
Sheet1
Cell Formulas
RangeFormula
G1G1=TEXT(TODAY(),"mmmm")
L1L1=TEXT(EDATE(TODAY(),BA1+1),"mmmm")
Q1Q1=TEXT(EDATE(TODAY(),BA1+2),"mmmm")
V1V1=TEXT(EDATE(TODAY(),BA1+3),"mmmm")
AA1AA1=TEXT(EDATE(TODAY(),BA1+4),"mmmm")
C3C3=IF(OR(B3="",B3="TBD"),"",B3+7)
G21G21=MONTH(TODAY())
H21:K21H21=G21+1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E3Expression=$C3="Bob"textNO
E3Expression=$C3="Bob"textNO
E3Expression=$C3="Andrew"textNO
E3Expression=$C3="Amber"textNO
E3Expression=$C3="Robert"textNO
E3Expression=$C3="Andrew"textNO
E3Expression=$C3="Amber"textNO
A3:E10Expression=$C3="Bob"textNO
A3:E10Expression=$C3="Bob"textNO
A3:E10Expression=$C3="Andrew"textNO
A3:E10Expression=$C3="Amber"textNO
A3:E10Expression=$C3="Robert"textNO
A3:E10Expression=$C3="Andrew"textNO
A3:E10Expression=$C3="Amber"textNO


Thanks for the help.
 
Try this

In which row do you have the names of the months?
Check that the names of the months are in row 1 and that they are the full names of the month and are in text.

If they are on another line, then change the row number on this line of the macro
Set f = Rows(1).Find(m, , xlValues, xlWhole)

It is very important that the name of the month is a text. As you put it in your examples.

VBA Code:
Sub Column_color_fill()
  Dim lr As Long, c As Range, f As Range, m As String, n As Long
  
  lr = ActiveSheet.Range("J:N").Find("*", , xlValues, , xlByRows, xlPrevious).Row
  Range("R5:AP" & lr).ClearContents
  Range("R5:AP" & lr).Interior.Color = xlNone
  
  For Each c In Range("J5:N" & lr).SpecialCells(xlCellTypeConstants)
    If c.Value <> "" And IsDate(c.Value) Then
      m = MonthName(Month(c.Value))
      n = WorksheetFunction.RoundUp(Day(c.Value) / 6, 0) - 1
      If n > 4 Then n = 4
      Set f = Rows(1).Find(m, , xlValues, xlWhole)
      If Not f Is Nothing Then
        With Cells(c.Row, f.Column + n)
          .Value = c.Value
          .Interior.Color = Cells(1, c.Column).Interior.Color
        End With
      End If
    End If
  Next
End Sub
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Dante,

I had the 1st and 2nd row merged together. I have unmerged them and made some minor formatting adjustments and it is working much better.

In my actual table, Date 3 is a formula that adds one week (7 days) to the value in Date 2. This date is not getting updated on the page with either color or date text in the cell. Everything else is updating as expected. All of the date columns are formatted as dates. What is the issue with this date not getting added to the table?

Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,697
Members
449,117
Latest member
Aaagu

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