change text color when the value is changed in a column

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
696
Office Version
  1. 2019
Platform
  1. Windows
Hello,
Looking to apply a text color (via Conditional Formatting or Macro) when the value changes in a column.
2,3 & 4 will be colored, only the First value.
Thank you

Book1
A
11
21
32
42
53
63
74
84
Sheet1
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this on a copy of your Workbook.
VBA Code:
Option Explicit

Sub ChangeFirstNewValueColor()
Dim rng As Range, cell As Range
Set rng = ActiveSheet.UsedRange
On Error Resume Next
For Each cell In rng
    If Not cell.Value = cell.Offset(-1, 0).Value Then cell.Font.Color = vbRed
Next cell
End Sub
 
Upvote 0
Avoid use of On Error statement,
Code:
Sub test()
    Dim r As Range, temp
    With Range("a1", Range("a" & Rows.Count).End(xlUp))
        .Font.ColorIndex = xlAutomatic
        For Each r In .Cells
            If r <> temp Then r.Font.Color = vbRed: temp = r
        Next
    End With
End Sub
 
Upvote 0
Try this on a copy of your Workbook.
VBA Code:
Option Explicit

Sub ChangeFirstNewValueColor()
Dim rng As Range, cell As Range
Set rng = ActiveSheet.UsedRange
On Error Resume Next
For Each cell In rng
    If Not cell.Value = cell.Offset(-1, 0).Value Then cell.Font.Color = vbRed
Next cell
End Sub
It works. Thank you.
How can I adjust so it acts on the row?
The column to look for the change is "D", when that value changes.

Book1
ABCDE
1Header1Header2Header3Header4Header5
2ORL7no1same
3MIL-7no1diff
4DAL4yes1diff
5OKC-4no2diff
6ATL7.5no2same
7NYK-7.5yes2same
8GSW2yes3same
9HOU-2yes3diff
10DET13.5yes3diff
11BOS-13.5no3diff
12TOR11no4diff
13MIA-11no4diff
14SAC-5.5yes4same
15NOP5.5yes4same
Sheet1
 
Upvote 0
Avoid use of On Error statement,
Code:
Sub test()
    Dim r As Range, temp
    With Range("a1", Range("a" & Rows.Count).End(xlUp))
        .Font.ColorIndex = xlAutomatic
        For Each r In .Cells
            If r <> temp Then r.Font.Color = vbRed: temp = r
        Next
    End With
End Sub
Thank you. I asked the previous responder if we could act on entire row rather than just a single cell.
 
Upvote 0
You mean like this?
Code:
Sub test()
    Dim r As Range, temp
    With [a1].CurrentRegion
        .Font.ColorIndex = xlAutomatic
        For Each r In .Columns(5).Offset(1).Resize(.Rows.Count - 1).Cells
            If r <> temp Then .Rows(r.Row - .Row + 1).Font.Color = vbRed: temp = r
        Next
    End With
End Sub
 
Upvote 0
Solution
You mean like this?
Code:
Sub test()
    Dim r As Range, temp
    With [a1].CurrentRegion
        .Font.ColorIndex = xlAutomatic
        For Each r In .Columns(5).Offset(1).Resize(.Rows.Count - 1).Cells
            If r <> temp Then .Rows(r.Row - .Row + 1).Font.Color = vbRed: temp = r
        Next
    End With
End Sub
Yes. I adjusted the Column(5) to (4) so its looking at the column for the change. Thank you.
Book1
ABCDE
1Header1Header2Header3Header4Header5
2ORL7no1same
3MIL-7no1diff
4DAL4yes1diff
5OKC-4no2diff
6ATL7.5no2same
7NYK-7.5yes2same
8GSW2yes3same
9HOU-2yes3diff
10DET13.5yes3diff
11BOS-13.5no3diff
12TOR11no4diff
13MIA-11no4diff
14SAC-5.5yes4same
15NOP5.5yes4same
Sheet1
 
Upvote 0
Avoid use of On Error statement,
Code:
Sub test()
    Dim r As Range, temp
    With Range("a1", Range("a" & Rows.Count).End(xlUp))
        .Font.ColorIndex = xlAutomatic
        For Each r In .Cells
            If r <> temp Then r.Font.Color = vbRed: temp = r
        Next
    End With
End Sub
@Fuji , I agree but had to in this case because there is no 0 row. I could've thrown an If statement in there, but I was feeling lazy. :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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