VBA to colour cells if they are equal to or greater than another worksheet cell value


New Member
Jun 8, 2015
Hi All,

Please could you help me. I am trying to loop through the rows in my worksheet and then if the cells in a column match a value in another worksheets cell then I need it to change to colour of the cell.

My Main table has about 50 rows and the headers start in row 2. There are 23 Columns (upto column "W")

So far I have made this code which almost works. However it doesn't get to the end row and misses the bottom 10. (There are about 50 rows). Once I get this code to work I need to copy it and do the same for 5 other columns in the same table but the cell values will be from various worksheets in the same Workbook.

Private Sub Worksheet_Activate()
Dim MyWorkbook As Workbook
Dim MySummary As Worksheet
Dim MyData As Worksheet
Dim cell As Range
Dim Rng As Range
Dim lastRow1 As Long
Dim color_green As Long
Dim color_red As Long
Dim color_blue As Long

Set MyWorkbook = ActiveWorkbook
Set MySummary = MyWorkbook.Sheets("Main")
Set MyData = MyWorkbook.Sheets("DataSheet1")

lastRow1 = MySummary.UsedRange.Rows.Count 'This is a better function to get the last used row (though there are disagreements on this)

color_green = 10
    ActiveWorkbook.Colors(color_green) = RGB(155, 187, 89)
color_red = 9
    ActiveWorkbook.Colors(color_red) = RGB(192, 80, 77)
color_blue = 8
    ActiveWorkbook.Colors(color_blue) = RGB(141, 180, 226)

With MySummary
    Set Rng = .Range(.Range("A3"), .Range("A" & lastRow1).End(xlUp))
End With

For Each cell In Rng
    If cell.Offset(, 3).Value > MyData.Range("F3").Value Then
        cell.Offset(, 3).Interior.ColorIndex = 10
        cell.Offset(, 3).Interior.ColorIndex = 9
    End If

End Sub
I would appreciate any assistance. Thank you.
Last edited:

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Eric W

MrExcel MVP
Aug 18, 2015
Are there any empty cells in your range? The xlUp function only works properly if there are no gaps. You can probably just use:

Set Rng = .Range(.Range("A3"), .Range("A" & lastRow1))

to set the range.

Also, that routine looks fairly simple. I'd bet that you could use Conditional Formatting instead.

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...