Forum Discussion

TattyJJ's avatar
TattyJJ
Copper Contributor
Apr 06, 2020

VBA To Highlight Rows

Hi 

We have a piece of software that outputs an Excel file once a week, this file has to be organised and edited to show specific information, which is quite a tedious task!
I am trying to produce a macro that will do everything for me, and have almost got everything figured out except the last little bit.

I need it to highlight all rows that have a date before today in column E
This bit of code i have been trying sort of works:

 

Dim TDateM As String
  TDateM = Date

endrow = Range("E" & Rows.Count).End(xlUp).Row

For Each cell In Range("E2:E" & endrow)
  If cell.Value < TDateM Then
     cell.EntireRow.Interior.ColorIndex = 6
  End If
Next

 


It has two small problems though, firstly it only highlights the rows prior to today for this month, anything last month or previous it doesn't seem to work for. This is a bit odd and i don't really understand why it would do this.
Secondly, it highlights the row of the whole workbook, i really want it to only highlight the row in the table. (Columns A to T if that helps)

Anyone able to help?


Ta muchly

8 Replies

  • mtarler's avatar
    mtarler
    Silver Contributor
    BTW I did some testing that way and it appears that could be the problem and you could solve the problem by using cdate(cell.value)
    • TattyJJ's avatar
      TattyJJ
      Copper Contributor

      mtarler 

      GOT IT!!!

      By converting all the dates in column E to number format, then using CLng(Date) to do the comparison with all the dates as integers, then turning them back into dates.

      Seems a bit long winded, but it does the job...


      Thanks for your help!

  • mtarler's avatar
    mtarler
    Silver Contributor
    It highlights the whole row because you selected cell.ENTIREROW.Interior…
    If you know it is columns A:T then you could use
    range("$a$" & cell.row & ":$T$" & cell.row).interior.colorindex = 6

    as for the rows only being this month without seeing the book my thought is there may be a 'break' in data between the months causing your .End(xlup) to only select to that point. If you want it to go all the way up to row 2 then just explicitly set that.

    That all said, this all seems like a simple conditional formatting rule. Have you tried that?
    • TattyJJ's avatar
      TattyJJ
      Copper Contributor

      mtarler 

      Thanks, that seems to have at least get it highlighting just the table.

      The are no breaks in the data, other than they are not consecutive.

      Quick screen shot of the data:


      Conditional formatting won't work as it is a new spreadsheet that is generated every week and has to be altered to produce a report.

      • mtarler's avatar
        mtarler
        Silver Contributor
        looks like you are from Europe with that day/month/year format. Is there any chance the comparison is not treating it as a date value but a text value instead?

Resources