Instant update of cell depending on value in other cell

WatfordKev

Board Regular
Joined
Mar 30, 2011
Messages
78
I want to do the following:

  • Update a cell (number).
  • Then compare against 2 others. If the input value is:
  • lower than or the first comparator, then a new cell = "red".
  • equal to or between the comparators, then a new cell = "amber".
  • above the second comparator, then the new cell = "green".
So an example would be

A2 = 5
A3 = 10

If I enter a value of 4 into A1, then cell A4 = "red"
If I enter a value of 7 into A1, then cell A4 = "amber"
If I enter a value of 11 into A1, then cell A4 = "green"

The trick is, I want to to this in code, in a macro, and want this to happen as soon as I put the value in i.e. "on the fly". Is this possible?

And once I have the code, how do I 'attach' it to the workbook? Thanks!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try the following worksheet event code by right-clicking on the specific sheet tab and then selecting view code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Application.EnableEvents = False
    Select Case Target.Value2
    Case 4
    Range("A4").Interior.Color = vbRed
    Case 7
    Range("A4").Interior.Color = vbBlue
    Case 11
    Range("A4").Interior.Color = vbGreen
    Case Else
    Range("A4").Interior.Color = vbWhite
    End Select
Application.EnableEvents = True
End If
End Sub
I have used vb available colors you can use .ColorIndex for the specific color you want!
 
Upvote 0
Hi,

You can use If then ElseIf structure to do this

Sub ColorCells()
If Range("A1").Value > 10 Then
Range("A4").interior.color = vbGreen
ElseIf Range("A1").value > 5 Then
Range("A4").interior.color = vbYellow
Elseif Range("A1").value > 0 Then
Range("A4").interior.color = Red
EndIf
End Sub

You can put this into the this worksheet
And select sheet change in top right corner
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,814
Members
452,945
Latest member
Bib195

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