Excel colour coding macro for formulas, constants

Anonymous
2015-08-07T05:42:09+00:00

Hello,

Can you help me create a macro that will color-code cells in excel (font colour) according to the following color-scheme (normally useful in financial models)?

Blue - Hard-coded values (numbers)

Black - Formulas

Red - Formulas with constant in them (for e.g. =sum(a2:a5)+1, a2*3.. etc) 

Green - References from another worksheet  / workbook

Ideally I would like to use a short cut key to run the macro for the entire workbook. I am not very familiar with macros or visual basic.

Thanks much for the help.

Karthik

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2015-08-07T11:21:55+00:00

    Perhaps this way if I have not made some mistake:

    Sub ColorDifCells()

    Signs = Array("=", "-", "+", "*", "/", "*")

    For Each Cell In ActiveSheet.UsedRange

      CF = Cell.Formula

      Cell.Font.Color = vbBlack

      If Not Cell.HasFormula Then

        Cell.Font.Color = vbBlue

      ElseIf InStr(1, CF, "!") Then

        Cell.Font.Color = vbGreen

      Else:

        For I = 2 To Len(CF)

          For J = LBound(Signs) To UBound(Signs)

          If IsNumeric(Mid$(CF, I, 1)) And Mid$(CF, I - 1, 1) = Signs(J) Then _

            Cell.Font.Color = vbRed: GoTo NextCell

          Next J

        Next I

      End If

    NextCell:

    Next

    End Sub

    'Blue - Hard-coded values (numbers)

    'Black -Formulas

    'Red - Formulas with constant in them (for e.g. =sum(a2:a5)+1, a2*3.. etc)

    'Green - References from another worksheet  / workbook

    Regards

    PB

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-08-15T06:10:52+00:00

    Thanks a ton!! This worked very well.

    0 comments No comments
  2. Anonymous
    2015-08-15T08:34:37+00:00

    Though the mistake, I had been afraid of, have crept into. The last '*' should be '('  -  for the case:   = (3+a2).

    Anyway I am glad even this served well.

    PB

    0 comments No comments