+ Reply to Thread
Results 1 to 15 of 15

Conditional formatting based on cell value in row above

Hybrid View

  1. #1
    Registered User
    Join Date
    08-12-2004
    Location
    Mendoza, Argentina
    MS-Off Ver
    Office Professional 2003
    Posts
    89

    Conditional formatting based on cell value in row above

    I am trying to conditional format all rows (in range) based on the value of a cell in the row above. I have got this code which is incorrect but I don't know why. Debugging indicates that there is a problem with the line starting Range("A2:M2).Cell .......... But it seems logical to me.

    Sub mycolour()
    Worksheets("Names").Activate
    Set myrange = Range("A2:M19")
    Range("A2:M2").Cell.EntireRow.Interior.Color = RGB(255, 255, 135)      'set colour of first data row - colour 1
        For Each Row In myrange                                                             ' check next row for colour
            If A3 = A2 Then
                Cell.EntireRow.Interior.Color = RGB(255, 255, 135)                ' colour 1
            Else
                Cell.EntireRow.Interior.Color = RGB(255, 204, 153)                ' colour 2
            End If
        Next
    End Sub
    If the value of A3 = value of A2 then row (or range) should be colour 1 else change colour to colour 2
    If the value of A4 = value of A3 then colour 1 or change to colour 2.

    Thus, rows 2, 3, 4,5 may be colour 1.
    Row 6 may be colour 2 because the value in A5 is not the same as A6
    Row 7 returns to colour 1 because A7 is not the same value as A6 ..... and so on.

    Where is the error in my code?

    Many thanks in advance.
    Last edited by thadacto; 02-14-2013 at 07:46 AM.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Conditional formatting based on cell value in row above

    Try something like this:

    Sub mycolour()
    
    Dim RowLoop As Range
    
    Worksheets("Names").Activate
    Set myrange = Range("A2:M19")
        For Each RowLoop In myrange.Rows                                                     ' check next row for colour
            If RowLoop.Cells(1) = RowLoop.Cells(1).Offset(-1) Then
                RowLoop.EntireRow.Interior.Color = RGB(255, 255, 135)                ' colour 1
            Else
                RowLoop.EntireRow.Interior.Color = RGB(255, 204, 153)                ' colour 2
            End If
        Next
    End Sub

  3. #3
    Registered User
    Join Date
    08-12-2004
    Location
    Mendoza, Argentina
    MS-Off Ver
    Office Professional 2003
    Posts
    89

    Re: Conditional formatting based on cell value in row above

    Thanks for the help, Andrew-R. However, the rows don't seem to be getting all the correct colours and I can't work out why. The first row in the range - row 2, should always be the same colour - colour 1 because the row above is a headder with the row name. However, depending on how the columns are sorted, row two is sometimes colour 2.

    Mystifying!!

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Conditional formatting based on cell value in row above

    OK, not quite sure why the value in row 2 should ever be the same as the header, but let's fudge it. Change:

    If RowLoop.Cells(1) = RowLoop.Cells(1).Offset(-1) Then
    To:

    If RowLoop.Cells(1) = RowLoop.Cells(1).Offset(-1) Or RowLoop.Row = myRange.Rows(1).Row Then

  5. #5
    Registered User
    Join Date
    08-12-2004
    Location
    Mendoza, Argentina
    MS-Off Ver
    Office Professional 2003
    Posts
    89

    Re: Conditional formatting based on cell value in row above

    Quote Originally Posted by Andrew-R View Post
    OK, not quite sure why the value in row 2 should ever be the same as the header, but let's fudge it. Change:

    If RowLoop.Cells(1) = RowLoop.Cells(1).Offset(-1) Then
    To:

    If RowLoop.Cells(1) = RowLoop.Cells(1).Offset(-1) Or RowLoop.Row = myRange.Rows(1).Row Then
    Still the same problem and not working correctly - all consecutive rows with the same col 1 value should be the same colour.

    I am attaching the file. Also, this needs to be initiated with a toggle button as when the sort criteria changes then the Ref column (column 1) changes but I have that part working (in another test file)
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Conditional formatting based on cell value in row above

    OK, with you now. How's this:

    Sub mycolour()
    
    Dim RowLoop As Range
    
    Worksheets("Names").Activate
    Set myrange = Range("A4:M19")
        For Each RowLoop In myrange.Rows                                            
            If RowLoop.Cells(1) = RowLoop.Cells(1).Offset(-1) And RowLoop.Row <> myrange.Rows(1).Row Then
                RowLoop.EntireRow.Interior.Color = RowLoop.Cells(1).Offset(-1).Interior.Color
            Else
              If RowLoop.Cells(1).Offset(-1).Interior.Color = RGB(255, 255, 135) Then
                RowLoop.EntireRow.Interior.Color = RGB(255, 204, 153)
              Else
                RowLoop.EntireRow.Interior.Color = RGB(255, 255, 135) 
              End If
            End If
        Next
    End Sub

  7. #7
    Registered User
    Join Date
    08-12-2004
    Location
    Mendoza, Argentina
    MS-Off Ver
    Office Professional 2003
    Posts
    89

    Re: Conditional formatting based on cell value in row above

    Unfortunately, that deleted the header row (row 1) and all the other rows were coloured with colour 1.

    Looking at the example,
    A2 is never the same value as A1 which is a header, therefore untrue and row 2 should be colour 2 (just thinking as I write, as 1 originally thought it should be colour 1)
    if A3 is the same value as A2, row 3 should be same colour as row 2 else change to other colour 1
    if A4 is the same value as A3, row 4 should be same colour as row 3 else change to other colour 1
    If we say for example,
    A5 is not same value as A4, row colour must change to colour 2

    Obviously more complicated than I thought - no wonder I couldn't work it out myself!
    Last edited by thadacto; 02-14-2013 at 11:34 AM.

  8. #8
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Conditional formatting based on cell value in row above

    I don't see how it can have deleted row 1, as there's nothing in there that references row 1 and certainly nothing to delete it.

    It worked fine on the example workbook you - attached is my screenshot after running it.
    Attached Images Attached Images

  9. #9
    Registered User
    Join Date
    08-12-2004
    Location
    Mendoza, Argentina
    MS-Off Ver
    Office Professional 2003
    Posts
    89

    Re: Conditional formatting based on cell value in row above

    Well that's amazing - I am not losing the header any more - but all rows in range are same colour.

    From your attached image, that is exactly what I want.

    Could you re-attach the file and I'll see if I can find any difference between yours and mine.

    many thanks for all your help and so prompt a reply as well.

  10. #10
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Conditional formatting based on cell value in row above

    Not a problem - attached.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    08-12-2004
    Location
    Mendoza, Argentina
    MS-Off Ver
    Office Professional 2003
    Posts
    89

    Re: Conditional formatting based on cell value in row above

    All rows in range change to yellow no rows in salmon as in your image.

    Are you using excel 2003 as I am?

  12. #12
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Conditional formatting based on cell value in row above

    No, I'm using Excel 2010 - maybe it is just an issue with which colours can be displayed, but even when I save that book as an Excel 2003 file it saves a close approximation of the colours used.

  13. #13
    Registered User
    Join Date
    08-12-2004
    Location
    Mendoza, Argentina
    MS-Off Ver
    Office Professional 2003
    Posts
    89

    Re: Conditional formatting based on cell value in row above

    The colours work OK - they're in the attachment that I posted, so it's not that. I'll have to study your code and see if I can understand it, I've never seen, let alone used RowLoop or offset(), the later I am assuming means the row above if offset(-1).

    I'll have to give it a rest for the moment and get out for my daily walk before it gets dark.

    Very many thanks for all your help, time and patience. Very much appreciated.

  14. #14
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Conditional formatting based on cell value in row above

    RowLoop is just a variable name, it's a loop going through all of the rows in the range. Offset is, as you've guessed, just a way to refer to a range a set number of rows and columns away from another one.

    I'll be online later on if you want to pick this up.

  15. #15
    Registered User
    Join Date
    08-12-2004
    Location
    Mendoza, Argentina
    MS-Off Ver
    Office Professional 2003
    Posts
    89

    Re: Conditional formatting based on cell value in row above

    Problem solved - For some reason, whether it's Excel 2003, or my computer, the RGB code for colours was not accepted. I changed it to ColorIndex= and the results came out perfectly. And using a toggle button to activate and deactivate the colour scheme makes life much easier and also easier to understand the whole worksheet.

    Many thanks again for all your time and trouble, Andrew-R.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1