Share via

Conditional formatting based on hardcoded, formula and links to other worksheets

Anonymous
2017-02-08T18:45:07+00:00

Is someone able to write me a macro that does the following:

Conditionally formats cells as follows:

Hardcoded cells (including cells that are a formula but with two hard coded numbers e.g. =50/10) --> blue font

Cells that contain ANY link to another worksheet -->green font

Cells that use a formula function (e.g. SUM, IF, HLOOKUP etc) BUT do not have any links to other worksheets --> black font

Is this possible? Would appreciate any tips on how to do this quicker on a mammoth spreadsheet if not.

Thanks

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

Answer accepted by question author

Anonymous
2017-02-09T16:20:16+00:00

Easy to rearrange the code to check for the link first:

Sub TestMacro2()

    Dim rngC As Range

    Dim rngA As Range

    Dim strA As String

    Set rngA = ActiveSheet.UsedRange

    rngA.SpecialCells(xlCellTypeConstants).Font.Color = vbBlue

    For Each rngC In rngA.SpecialCells(xlCellTypeFormulas)

        If InStr(1, rngC.Formula, "!") > 0 Then

            rngC.Font.Color = vbGreen

        Else

            On Error GoTo NextCheck

            strA = rngC.Precedents.Address

            rngC.Font.Color = vbBlack

            GoTo SheetRefsOnly

NextCheck:

            Resume CheckLinks

CheckLinks:

            rngC.Font.Color = vbBlue

        End If

SheetRefsOnly:

    Next rngC

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-02-08T21:31:55+00:00

    Give this a try:

    Sub TestMacro()

        Dim rngC As Range

        Dim rngA As Range

        Dim strA As String

        Set rngA = ActiveSheet.UsedRange

        rngA.SpecialCells(xlCellTypeConstants).Font.Color = vbBlue

        For Each rngC In rngA.SpecialCells(xlCellTypeFormulas)

            On Error GoTo NextCheck

            strA = rngC.Precedents.Address

            rngC.Font.Color = vbBlack

            GoTo SheetRefsOnly

    NextCheck:

            Resume CheckLinks

    CheckLinks:

            If InStr(1, rngC.Formula, "!") > 0 Then

                rngC.Font.Color = vbGreen

            Else

                rngC.Font.Color = vbBlue

            End If

    SheetRefsOnly:

        Next rngC

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2017-02-10T12:34:42+00:00

    Spot on!

    Thank you so much!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-02-09T08:26:46+00:00

    Thanks Bernie you're a modern day hero.

    This works perfectly except it is shading in black some HLOOKUP formulae that refer to a table array in a different worksheet which ideally I'd like to shaded green:

    =HLOOKUP($B$2,'OTHERWORKSHEET'!$B$2:$G$419,ROW()-1,FALSE)

    I'm being picky though you're a life saver!

    Was this answer helpful?

    0 comments No comments