Macro to Color Cell Font When Linking to Other Cells in Same Sheet

mp1021257

New Member
Joined
Mar 2, 2016
Messages
19
Hi,

I am trying to write a simple macro that will color a cell's font green if a cell links to another cell in the same worksheet. (I have come across macros online that will color a cell's font if it links to another cell in a separate worksheet or workbook, but unfortunately, nothing that solves my problem.

Simply put, I would like a macro that will evaluate if, for example, cell C2 is set equal to cell A2. If true, I would like the font of cell C2 to be green. Any easy way to do this?

Thank you for your help!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,

This might get you started:
Code:
Sub ColorDependents()
    Dim r As Range
    For Each r In ActiveSheet.UsedRange.Dependents
        r.Font.ColorIndex = 4
    Next
End Sub

Regards,
 
Upvote 0
Thanks, Rick. I appreciate the quick response.

Unfortunately, this gets me the same problem I was running into on my own. This colors formulas green as well. Thoughts on how to to make this apply only to cells that link to other cells, but do not have a formula in them?

I would like this to apply only to simple cases where I am setting on cell equal to another (e.g. C2 is set equal to A2, but not where C2 =SUM(A2,B2)).
 
Upvote 0
Would it be possible to add a constraint here that only colors the dependent green if the text of the formula matches an equals sign + the address of the dependent cell?

In other words, using my same example as before, if the following conditions are met: (1) cell A2 is a dependent of cell C2; and (2) the text of the formula in cell C2 is precisely "=A2" (or more generally "="&"[R1C1 address of variable dependent cell]", then color font green. If not, (i.e. text of the formula in cell C2 is something like "=SUM(A2)", then do not color the font.
 
Upvote 0
I want to color green only in situations where I am directly linking (i.e. C2 =A2). Not in any other situations (e.g. C2 =SUM(A2)).

Thanks.
 
Last edited:
Upvote 0
I am trying to distinguish between simple links to other cells on the same sheet and more complex formulas.
 
Upvote 0
You could do this:
Code:
Sub ColorDependents()
    Dim r As Range
    Dim s As Range
    
    For Each r In ActiveSheet.UsedRange.Dependents
        On Error Resume Next
        Set s = Range(Mid(r.Formula, 2))
        If Err.Number = 0 Then r.Font.ColorIndex = 4
    Next
End Sub
That colours all the cells with dependants but skips the ones that do not contain just a single cell reference.

If tries to set Range s to the Address. If it fails it assumes that the cell contents are therefore not to be coloured green.


Regards,
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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