ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Macro To Change Cell Color When Value Changes (https://www.excelbanter.com/excel-worksheet-functions/77221-macro-change-cell-color-when-value-changes.html)

carl

Macro To Change Cell Color When Value Changes
 
I have a spreadsheet that is tracking stock prices. The cell with the price
is fed by a DDE link.


Stock Price
AIG 100
AXP 100
AMAT 100
AMGN 100
AMD 100

I am trying to create a visual monitoring tool. I was hoping to have the
cell (ColB) change color (any color will do) everytime the price changes.

For example, if the cell initially is no color, on the first change, color
switches to green, on the next change, back to no color, on the 3rd price
change, back to green etc.

Is this type of thing possible ?

Thank you in advance.

JE McGimpsey

Macro To Change Cell Color When Value Changes
 
One way:

Put this in your worksheet code module (right-click the worksheet tab
and choose View Code):

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Const sRANGE As String = "B2:B100"
Const nCOLORINDEX As Long = 10 'Green
With Target
If .Count 1 Then Exit Sub
If Not Intersect(.Cells, Range(sRANGE)) Is Nothing Then
With .Interior
.ColorIndex = IIf(.ColorIndex = nCOLORINDEX, _
xlColorIndexNone, nCOLORINDEX)
End With
End If
End With
End Sub


In article ,
carl wrote:

I have a spreadsheet that is tracking stock prices. The cell with the price
is fed by a DDE link.


Stock Price
AIG 100
AXP 100
AMAT 100
AMGN 100
AMD 100

I am trying to create a visual monitoring tool. I was hoping to have the
cell (ColB) change color (any color will do) everytime the price changes.

For example, if the cell initially is no color, on the first change, color
switches to green, on the next change, back to no color, on the 3rd price
change, back to green etc.

Is this type of thing possible ?

Thank you in advance.


carl

Macro To Change Cell Color When Value Changes
 
Thank you very much.

I tried this out but could not get it to work . I have a formula in ColB.
Does that make a difference ?

"JE McGimpsey" wrote:

One way:

Put this in your worksheet code module (right-click the worksheet tab
and choose View Code):

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Const sRANGE As String = "B2:B100"
Const nCOLORINDEX As Long = 10 'Green
With Target
If .Count 1 Then Exit Sub
If Not Intersect(.Cells, Range(sRANGE)) Is Nothing Then
With .Interior
.ColorIndex = IIf(.ColorIndex = nCOLORINDEX, _
xlColorIndexNone, nCOLORINDEX)
End With
End If
End With
End Sub


In article ,
carl wrote:

I have a spreadsheet that is tracking stock prices. The cell with the price
is fed by a DDE link.


Stock Price
AIG 100
AXP 100
AMAT 100
AMGN 100
AMD 100

I am trying to create a visual monitoring tool. I was hoping to have the
cell (ColB) change color (any color will do) everytime the price changes.

For example, if the cell initially is no color, on the first change, color
switches to green, on the next change, back to no color, on the 3rd price
change, back to green etc.

Is this type of thing possible ?

Thank you in advance.



JE McGimpsey

Macro To Change Cell Color When Value Changes
 
Yes, that makes a difference - Worksheet_Change() only fires when a
cell's value is changed manually or via link.

Hard to tell what to change without knowing how your data is input...

In article ,
carl wrote:

I tried this out but could not get it to work . I have a formula in ColB.
Does that make a difference ?


carl

Macro To Change Cell Color When Value Changes
 
Thank you again. The price data in ColB is coming from a "DDE Link". This is
a "formula" that reads in streaming data from a data source.

Does that clarify ?

"JE McGimpsey" wrote:

Yes, that makes a difference - Worksheet_Change() only fires when a
cell's value is changed manually or via link.

Hard to tell what to change without knowing how your data is input...

In article ,
carl wrote:

I tried this out but could not get it to work . I have a formula in ColB.
Does that make a difference ?




All times are GMT +1. The time now is 11:50 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com