Share via

Conditional Formatting with Colored Icons using VBA

Anonymous
2016-12-13T00:54:28+00:00

Hi everyone,

I'm trying to implement some VBA code into my project that will display a red or green up/down arrow based on a certain condition.

My table looks like this:

Store Year A Values B Values C Values D Values
1 2015 0.0 0.0 1.8 2.6
1 2016 1.2 0.0 2.2 1.1

My current VBA does the following:

  • If the Store Number in column 1 matches, the value in columns 3-6 get compared. If the values in 2016 are greater/less than 2015, 2016's cell gets formatted by coloring the cell red or green. My current code is this:

Sub ColorYear()

Dim long1 As Long

Dim rng As Range

Set rng = ActiveSheet.Range("A:A")

long1 = Application.WorksheetFunction.CountA(rng)

For t = 2 To long1

If ActiveSheet.Range("A" & t).Value = ActiveSheet.Range("A" & t - 1).Value Then

    If ActiveSheet.Range("C" & t).Value < ActiveSheet.Range("C" & t - 1).Value Then

    ActiveSheet.Range("C" & t).Interior.Color = RGB(0, 255, 0)

    ElseIf ActiveSheet.Range("C" & t).Value > ActiveSheet.Range("C" & t - 1).Value Then

    ActiveSheet.Range("C" & t).Interior.Color = RGB(255, 0, 0)

    End If

    If ActiveSheet.Range("D" & t).Value < ActiveSheet.Range("D" & t - 1).Value Then

    ActiveSheet.Range("D" & t).Interior.Color = RGB(0, 255, 0)

    ElseIf ActiveSheet.Range("D" & t).Value > ActiveSheet.Range("D" & t - 1).Value Then

    ActiveSheet.Range("D" & t).Interior.Color = RGB(255, 0, 0)

    End If

    If ActiveSheet.Range("E" & t).Value < ActiveSheet.Range("E" & t - 1).Value Then

    ActiveSheet.Range("E" & t).Interior.Color = RGB(0, 255, 0)

    ElseIf ActiveSheet.Range("E" & t).Value > ActiveSheet.Range("E" & t - 1).Value Then

    ActiveSheet.Range("E" & t).Interior.Color = RGB(255, 0, 0)

    End If

    If ActiveSheet.Range("F" & t).Value < ActiveSheet.Range("F" & t - 1).Value Then

    ActiveSheet.Range("F" & t).Interior.Color = RGB(0, 255, 0)

    ElseIf ActiveSheet.Range("F" & t).Value > ActiveSheet.Range("F" & t - 1).Value Then

    ActiveSheet.Range("F" & t).Interior.Color = RGB(255, 0, 0)

    End If

End If

Next t

End Sub

What I'd like to do is instead of just coloring the cell red/green, I'd like to implement the colored up/down arrow icons. I have no idea how to write this out though, so I was hoping for some assistance.

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

HansV 462.6K Reputation points
2016-12-14T10:13:51+00:00

Like this:

Sub ColorYear()

    Dim long1 As Long

    Dim t As Long

    Dim c As Long

    Application.ScreenUpdating = False

    ' Last used row

    long1 = Cells(Rows.Count, 1).End(xlUp).Row

    ' Delete existing rules

    Range("B3:I" & long1).FormatConditions.Delete

    ' Loop through the rows

    For t = 3 To long1

        For c = 3 To 9 Step 2 ' columns C,E,G,I

            With Cells(t, c).FormatConditions.AddIconSetCondition

                .IconSet = ActiveWorkbook.IconSets(xl3Arrows)

                With .IconCriteria(2)

                    .Type = xlConditionValueFormula

                    .Value = "=" & Cells(t, c - 1).Address

                    .Operator = 7

                End With

                With .IconCriteria(3)

                    .Type = xlConditionValueFormula

                    .Value = "=" & Cells(t, c - 1).Address

                    .Operator = 5

                End With

            End With

        Next c

    Next t

    Application.ScreenUpdating = True

End Sub

Was this answer helpful?

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2016-12-13T07:18:15+00:00

Try this:

Sub ColorYear()

    Dim long1 As Long

    Dim t As Long

    Dim c As Long

    Application.ScreenUpdating = False

    ' Last used row

    long1 = Application.WorksheetFunction.CountA(Range("A:A"))

    ' Delete existing rules

    Range("C2:F" & long1).FormatConditions.Delete

    ' Loop through the rows

    For t = 2 To long1

        If Cells(t - 1, 1).Value = Cells(t, 1).Value Then

            For c = 3 To 6 ' columns C to F

                With Cells(t, c).FormatConditions.AddIconSetCondition

                    .IconSet = ActiveWorkbook.IconSets(xl3Arrows)

                    With .IconCriteria(2)

                        .Type = xlConditionValueFormula

                        .Value = "=" & Cells(t - 1, c).Address

                        .Operator = 7

                    End With

                    With .IconCriteria(3)

                        .Type = xlConditionValueFormula

                        .Value = "=" & Cells(t - 1, c).Address

                        .Operator = 5

                    End With

                End With

            Next c

        End If

    Next t

    Application.ScreenUpdating = True

End Sub

Was this answer helpful?

0 comments No comments

18 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-12-13T16:45:57+00:00

    No worries, I just reversed the icon order which seems to serve my purposes.

    Thank you again Hans!!

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2016-12-13T16:43:11+00:00

    Sorry, no. The icon sets are fixed, and the red arrow is always down and the green one is up.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-12-13T16:23:24+00:00

    Thank you Hans!!! This is just about perfect.

    Is it possible to change the colors of the arrows? For instance, instead of a red down arrow, switch it to a green down arrow and red up arrow?

    Was this answer helpful?

    0 comments No comments