Consulting

Results 1 to 3 of 3

Thread: Conditional formating of rows

  1. #1
    VBAX Regular
    Joined
    Jan 2011
    Posts
    18
    Location

    Conditional formating of rows

    Hi,

    I have data in cells A2 to K5000. I want to colour the entire rows based on criteria being met in column A.

    As an example, let’s assume the following values in the first few of rows in A2:A9:

    7
    7
    12
    12
    12
    4
    11
    11

    I want to alternative row colours based on groups of cells as indentified by where consecutive rows contain the same values in column A.

    So for instance I want to colour the first group of cells white (the 7s - a2:a3), the next set (the 12s) green, the next set white (just one row here - 4), the next set green (11s) etc for all groups in the entire range.

    This is to help visually breakout the groups. Note the colour of the cell should not be conditional on the actual value of the cell, but by being in distinct different groups as indentified by immediate cells having the same value and the actual colour is not really important.

    Anyone know how best to do this?

    Thanks,

    XLUser

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    this may help:

    http://www.mrexcel.com/forum/showthread.php?t=174759


    [vba]
    Option Explicit
    Option Base 1

    Sub color_same_data()
    'Erik Van Geit
    '051124 1754
    'color cells with same contents in single column

    Dim rng As Range
    Dim LR As Long
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim Arr As Variant
    Dim ArrItem As String
    Dim ArrRowNumbers() As Variant
    Dim HL As Integer 'highlight

    Const col = 1
    Const FR = 2

    If Cells(Rows.Count, col) <> "" Then LR = Rows.Count Else LR = Cells(Rows.Count, col).End(xlUp).Row
    Set rng = Range(Cells(FR, col), Cells(LR, col))

    Arr = rng.Value
    i = 1
    j = 1
    Do
    ArrItem = Arr(i, 1)
    k = i
    On Error Resume Next 'avoids bug at the end of the loop "Arr(i, 1)" when i > UBound(arr)
    Do
    i = i + 1
    Loop While ArrItem = Arr(i, 1)
    On Error GoTo 0
    'If k <> i - 1 Then 'enable these line to skip singles
    ReDim Preserve ArrRowNumbers(j + 1)
    ArrRowNumbers(j) = k + FR - 1
    ArrRowNumbers(j + 1) = i - 1 + FR - 1
    j = j + 2
    'End If 'enable these line to skip singles
    Loop While i < LR


    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    End With

    For i = 1 To j - 1 Step 2
    HL = IIf(HL = 35, 36, 35)
    Range(Cells(ArrRowNumbers(i), col), Cells(ArrRowNumbers(i + 1), col)).Interior.ColorIndex = HL
    Next i

    With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    End With

    End Sub[/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Conditional formatting using a Helper column
    Attached Files Attached Files
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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