VB code for conditional formatting on live value cell?

slimimi

Well-known Member
Joined
May 27, 2008
Messages
532
Hi there,\

Sorry for my long Title description - hope i got it right !! :)

Is there a way to look at a cell's own value and then format it accordingly using VB?

eg,

I have a cell whose value changes based on DDE link to live price data.
Cell is "Q5".

When the new value is greater than the old value - i want to format the pattern color of the cell.

When the new value is less than the old value - i want to format the pattern color of the cell.

Does anyone know how to do this please or is there a way to do this using spreadsheet formulas as oppose to VB?

THanks in advance for looking in..
 
Thank you Tom. I assume this method is doing away with the textboxes - correct?

I have changed all the code you mentioned and also changed the formulas in the cells S5, S8, etc...

I have saved this as a new workbook called "Test".
However, when i reopen this "Text" workbook i always get the following message:

Remote Data not accessible.
Do you want to start DDESERVER.EXE?

This is the code i have so far from you with the changes :
Option Explicit

Private LinkRanges(6) As Range
Private PreviousLinkRangeValues(6) As Currency

Private Sub Workbook_Open()
StartWatchingLinks
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
StopWatchingLinks
End Sub

Friend Sub StartWatchingLinks()
Dim x As Integer

On Error Resume Next
For x = 0 To 6
With Me.Worksheets("positions")
Set LinkRanges(x) = .Range(Choose(x + 1, "S5", "S8", "S11", "S14", "S17", "S20", "S23"))
PreviousLinkRangeValues(x) = LinkRanges(x).Value
End With
Next

ThisWorkbook.SetLinkOnData "MT4|BID!AUDUSDm", "'ThisWorkbook.OnLinkUpdate ""0""'"
ThisWorkbook.SetLinkOnData "MT4|BID!EURAUDm", "'ThisWorkbook.OnLinkUpdate ""1""'"
ThisWorkbook.SetLinkOnData "MT4|BID!EURGBPm", "'ThisWorkbook.OnLinkUpdate ""2""'"
ThisWorkbook.SetLinkOnData "MT4|BID!EURUSDm", "'ThisWorkbook.OnLinkUpdate ""3""'"
ThisWorkbook.SetLinkOnData "MT4|BID!GBPUSDm", "'ThisWorkbook.OnLinkUpdate ""4""'"
ThisWorkbook.SetLinkOnData "MT4|BID!USDCHFm", "'ThisWorkbook.OnLinkUpdate ""5""'"
ThisWorkbook.SetLinkOnData "MT4|BID!USDJPYm", "'ThisWorkbook.OnLinkUpdate ""6""'"
End Sub

Friend Sub StopWatchingLinks()
ThisWorkbook.SetLinkOnData "MT4|BID!AUDUSDm", ""
ThisWorkbook.SetLinkOnData "MT4|BID!EURAUDm", ""
ThisWorkbook.SetLinkOnData "MT4|BID!EURGBPm", ""
ThisWorkbook.SetLinkOnData "MT4|BID!EURUSDm", ""
ThisWorkbook.SetLinkOnData "MT4|BID!GBPUSDm", ""
ThisWorkbook.SetLinkOnData "MT4|BID!USDCHFm", ""
ThisWorkbook.SetLinkOnData "MT4|BID!USDJPYm", ""
End Sub

Friend Sub OnLinkUpdate(LinkIndex As Integer)
On Error GoTo ErrOnLinkUpdate
If LinkRanges(LinkIndex).Value > PreviousLinkRangeValues(LinkIndex) Then
LinkRanges(LinkIndex).Interior.Color = vbBlue
ElseIf LinkRanges(LinkIndex).Value < PreviousLinkRangeValues(LinkIndex) Then
LinkRanges(LinkIndex).Interior.Color = vbRed
End If
PreviousLinkRangeValues(LinkIndex) = LinkRanges(LinkIndex).Value
ErrOnLinkUpdate:
End Sub

Is it correct what i have done?

I can't see where, in the code, it is referencing to DDESERVER - so i am not quite sure why its still asking me if i want to start that application.

Thanks in advance for your amazing help and kindness Tom.

Best Regards,
Imran
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Are you sure that you removed all of the links that have DDESERVER in them? Do a CTRL-F on your worksheet and search for DDESERVER. The cell links need to be the same as

MT4|BID!AUDUSDm
MT4|BID!EURAUDm
...
 
Upvote 0
Tom can i be a BIG PAIN now and ask you if its possible, for each currency's live price data, to keep everything the same (as in "S5, S8", etc) as the link data BUT to change the target cell for the color changes, ie, Cell A5 change color for Cell S5 link data, Cell A8 change color for Cell S8 link data, etc, etc...

Reason why i ask is because i realized i have conditional formatting on Column S which sort of acts as an alert for me (from glancing at the screen from a distance) to show me if i need to tell the kids to play and spend 3 minutes investigating that trade further (he he).. Hope this makes sense.

So i think if A5 gives me the indication of whether price is moving up or down from previous price - this would be better.

Thanks a million in advance...
 
Upvote 0
Are you sure that you removed all of the links that have DDESERVER in them? Do a CTRL-F on your worksheet and search for DDESERVER. The cell links need to be the same as

MT4|BID!AUDUSDm
MT4|BID!EURAUDm
...

I did what you suggested Tom but Excel did not find any matches.
Hmmm - this is really weird huh !!!
 
Upvote 0
Code:
Friend Sub OnLinkUpdate(LinkIndex As Integer)
    On Error GoTo ErrOnLinkUpdate
    If LinkRanges(LinkIndex).Value > PreviousLinkRangeValues(LinkIndex) Then
         LinkRanges(LinkIndex).Offset(, -18).Interior.Color = vbRed
    ElseIf LinkRanges(LinkIndex).Value < PreviousLinkRangeValues(LinkIndex) Then
        LinkRanges(LinkIndex).Offset(, -18).Interior.Color = vbBlue
    End If
    PreviousLinkRangeValues(LinkIndex) = LinkRanges(LinkIndex).Value
ErrOnLinkUpdate:
End Sub

Did you see my previous reply? Were you able to get rid of the references to DDESERVER?
 
Upvote 0
Run this from the VBAIDE and hit CTRL-G5 to bring up the immediates window. Paste the results in your next repy...

Code:
Sub RunThis()
    Dim aLinks, i
    aLinks = ActiveWorkbook.LinkSources(xlOLELinks)
    If Not IsEmpty(aLinks) Then
        For i = 1 To UBound(aLinks)
            Debug.Print "Link " & i & ":" & Chr(13) & aLinks(i)
        Next i
    End If
End Sub


BTW. For your offset color in column A
Code:
Friend Sub OnLinkUpdate(LinkIndex As Integer)
    On Error GoTo ErrOnLinkUpdate
    If LinkRanges(LinkIndex).Value > PreviousLinkRangeValues(LinkIndex) Then
         LinkRanges(LinkIndex).Offset(, -18).Interior.Color = vbRed
    ElseIf LinkRanges(LinkIndex).Value < PreviousLinkRangeValues(LinkIndex) Then
        LinkRanges(LinkIndex).Offset(, -18).Interior.Color = vbBlue
    End If
    PreviousLinkRangeValues(LinkIndex) = LinkRanges(LinkIndex).Value
ErrOnLinkUpdate:
End Sub
 
Last edited by a moderator:
Upvote 0
Hi tom - thank you.
I am sorry - i dont know how to run VBAIDE.
Please would you be so kind as to advise me on this.

THanks in advance.
 
Upvote 0
From a worksheet. ALT-F11 to open the VBAIDE. Paste in the macro, press CTRL-G to show the immediates window or click on View-Immediate Window. Run this:
Code:
Sub RunThis()
    Dim aLinks, i
    aLinks = ActiveWorkbook.LinkSources(xlOLELinks)
    If Not IsEmpty(aLinks) Then
        For i = 1 To UBound(aLinks)
            Debug.Print "Link " & i & ":" & Chr(13) & aLinks(i)
        Next i
    End If
End Sub

Debug.Print "Link " & i & ":" & Chr(13) & aLinks(i)
this line will output to the immediates window.
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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