Forum Discussion

Anders_Svarre's avatar
Anders_Svarre
Copper Contributor
Jun 24, 2022
Solved

Color change with new values

Hi.   I'm trying format my sheet so that it changes color every time a line with a new value is reached. The table I'm working with will have a random number of lines with the same value in column ...
  • OliverScheurich's avatar
    OliverScheurich
    Jun 27, 2022

    Anders_Svarre 

    Sub color()
    
    Dim i As Double
    Dim k As Integer
    Dim MaxRow As Double
    
    Range("A:A").Interior.ColorIndex = xlNone
    
    MaxRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 2 To MaxRow
    
    If Cells(i, 1).Value = Cells(i + 1, 1).Value Then
    
    Cells(i, 1).Interior.ColorIndex = 4 + k
    Cells(i + 1, 1).Interior.ColorIndex = 4 + k
    
    Else
    
    k = k + 1
    
    If k = 50 Then
    k = 4
    Else
    End If
    
    If Cells(i + 1, 1).Value = "" Then
    Else
    Cells(i + 1, 1).Interior.ColorIndex = 4 + k
    End If
    
    End If
    
    Next i
    
    End Sub

    Sorry for the mistake. In the attached file i've tested the code for more than 40000 rows and it should work now.

     

    Meanwhile you've already received two easier solutions from HansVogelaar .

     

Resources