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

zaincmt

New Member
Joined
Jun 8, 2015
Messages
14
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.

Code:
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
    Else
        cell.Offset(, 3).Interior.ColorIndex = 9
    End If
Next

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

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
9,888
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

Threads
1,102,039
Messages
5,484,329
Members
407,436
Latest member
Szafranski

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top