Macro that will color selected cells green if they refer to other worksheet

lifeskillz

New Member
Joined
Jul 26, 2011
Messages
5
I am trying to create a macro that will color a selected range of cells green (or any other color for that matter) if the formula in that cell refers to a value in another worksheet.

My current macro colors a cell blue if it has a hardcoded value or black if it refers to another cell within that worksheet. I just don't know how to color the cell green if the formula refers to a cell in another worksheet.

Can anyone help? Thank you!
 
I do suggest you take a look at my Mappit! addin I referenced earlier if you get a chance

While I know you are okay with the false positives for now, my addin will catch these

Cheers

Dave
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi Peter,

I've been searching online for a similar macro. I'm looking for something that will color:

1) black if it contains any formula
2) green if it's a simple reference to another cell
3) blue if it's a hard code

This is slightly different from what was described above. Any assistance would be much appreciated!

so if it's =2+2 it would be black. If it's =C2+5 it would be black.
if it's =C2 it would be green
 
Upvote 0
Hi Peter,

I've been searching online for a similar macro. I'm looking for something that will color:

1) black if it contains any formula
2) green if it's a simple reference to another cell
3) blue if it's a hard code

This is slightly different from what was described above. Any assistance would be much appreciated!

so if it's =2+2 it would be black. If it's =C2+5 it would be black.
if it's =C2 it would be green
I'm sorry for the slow response but I have been away from the forum for quite a while. If you are still interested, you could try this adaptation of my earlier macro.
Code:
Sub ColourCells()
    Dim cell As Range, SCC As Range, SCF As Range, Test As Range
    Dim CI As Long
    
    With ActiveSheet.UsedRange
        On Error Resume Next
        Set SCC = .SpecialCells(xlCellTypeConstants)
        Set SCF = .SpecialCells(xlCellTypeFormulas)
        On Error GoTo 0
    End With
    If Not SCC Is Nothing Then
        SCC.Font.ColorIndex = 5
    End If
    If Not SCF Is Nothing Then
        For Each cell In SCF
            Set Test = Nothing
            On Error Resume Next
            Set Test = Range(Mid(cell.Formula, 2))
            On Error GoTo 0
            If Test Is Nothing Then
                CI = 1
            Else
                CI = 4
            End If
            cell.Font.ColorIndex = CI
        Next cell
    End If
End Sub
 
Upvote 0
I'm sorry for the slow response but I have been away from the forum for quite a while. If you are still interested, you could try this adaptation of my earlier macro.
Code:
Sub ColourCells()
    Dim cell As Range, SCC As Range, SCF As Range, Test As Range
    Dim CI As Long
    
    With ActiveSheet.UsedRange
        On Error Resume Next
        Set SCC = .SpecialCells(xlCellTypeConstants)
        Set SCF = .SpecialCells(xlCellTypeFormulas)
        On Error GoTo 0
    End With
    If Not SCC Is Nothing Then
        SCC.Font.ColorIndex = 5
    End If
    If Not SCF Is Nothing Then
        For Each cell In SCF
            Set Test = Nothing
            On Error Resume Next
            Set Test = Range(Mid(cell.Formula, 2))
            On Error GoTo 0
            If Test Is Nothing Then
                CI = 1
            Else
                CI = 4
            End If
            cell.Font.ColorIndex = CI
        Next cell
    End If
End Sub

Peter, this is amazing! works perfectly! Thank you!
 
Upvote 0
These conditions are not exhaustive since a formula can exist that does not meet any of those conditions (eg =4*2)

The conditions are also not mutually exclusive - see my first paragraph in post #3. To emphasise - a single formula can refer to ..

a) a cell within the current sheet and a cell in another workbook
b) a cell in another worksheet of the current workbook and a cell in another workbook
c) a cell within the current sheet and a cell in another worksheet of the current workbook and a cell in another workbook
etc

So, for example, if one of the above examples occur, what colour to use?

Taking a guess that ..
If another workbook is involved at all, use red.
Otherwise if another worksheet is involved at all, use green
Otherwise, use black
.. then this may be some use.


Sub AutoColorSelection()
****Dim cell As Range, SCC As Range, SCF As Range
****Dim CI As Long
****
****With Selection
********On Error Resume Next
********Set SCC = .SpecialCells(xlCellTypeConstants, xlNumbers)
********Set SCF = .SpecialCells(xlCellTypeFormulas)
********On Error GoTo 0
****End With
****If Not SCC Is Nothing Then
********SCC.Font.ColorIndex = 5
****End If
****If Not SCF Is Nothing Then
********For Each cell In SCF
************If cell.Formula Like "*.xls*]*!*" Then
****************CI = 3
************ElseIf cell.Formula Like "*!*" Then
****************CI = 50
************Else
****************CI = 0
************End If
************cell.Font.ColorIndex = CI
********Next cell
****End If
End Sub


Hey Peter, just wanted to thank you for this macro, it has been an insane help. I was wondering how we would do something similar if I wanted to italicize all percentages on a worksheet? Maybe we could work that into the code above. I've been struggling to figure this out, but i'm no VBA expert.
 
Upvote 0
.. if I wanted to italicize all percentages on a worksheet?
Welcome to the MrExcel board!

Does this do what you want?
Rich (BB code):
Sub Percent_Italic()
  Dim cell As Range
  
  For Each cell In ActiveSheet.UsedRange
      If cell.NumberFormat Like "*%" Then cell.Font.Italic = True
  Next cell
End Sub
 
Upvote 0
Welcome to the MrExcel board!

Does this do what you want?
Rich (BB code):
Sub Percent_Italic()
  Dim cell As Range
  
  For Each cell In ActiveSheet.UsedRange
      If cell.NumberFormat Like "*%" Then cell.Font.Italic = True
  Next cell
End Sub

Yes, this is perfect... just wondering if there is a way to tie this into the color coding one for one overall macro.

Thanks so much, you have been a huge help. :)
 
Upvote 0
Hey Peter, this was so helpful! I was wondering if you can help me tweak it a little bit. I need to have cells that reference the same work sheet as green, and cells that reference other worksheets as black. Also is there any way of including something to be able to automatically update the color, say I had a formula which would make it black, then I decide to hard code that cell, is there any way to turn it blue automatically without running the macro again?

Thank you
Rachel
 
Upvote 0
Clarifying, you want ...

- blue if hardcoded (constant)?

- green if a formula references its own worksheet?

- black if a formula references other worksheet(s)?

1. What is to happen if a cell references the same worksheet AND another worksheet?
eg =VLOOKUP(D2,'Budget Worksheet'!A2:B10,2,0)

2. What is to happen if a cell contains a formula that doesn't reference any cells at all?
eg =2+2
 
Last edited:
Upvote 0
Peter,

Thanks for your prompt response! Answers to your questions:

1) I would just be black, since it's a formula. I would only want it to be green if it directly links to the active worksheet, otherwise black.
2) It would still be black since it's a formula

Text would also be black, formatting would only apply to numbers

Thank you for your time.

Rachel
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,179
Members
448,871
Latest member
hengshankouniuniu

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