Consulting

Results 1 to 3 of 3

Thread: Solved: multiple interior.colorindex

  1. #1
    VBAX Newbie
    Joined
    Jul 2011
    Posts
    3
    Location

    Solved: multiple interior.colorindex

    Hi all,

    I would like to color multi cells on a row at once without to repeat for each:

    Dim Xcell As Range
    For Each Xcell In Range("L8:R100")

    If Cells(Xcell.Row, 15).Value = "x" Then
    Cells(Xcell.Row, 33).Interior.ColorIndex = 3
    Cells(Xcell.Row, 34).Interior.ColorIndex = 3
    Cells(Xcell.Row, 35).Interior.ColorIndex = 3
    Cells(Xcell.Row, 36).Interior.ColorIndex = 3
    ...

    Thanks!
    Luca

  2. #2
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    There are many ways to achieve this:
    First is something you are already doing:
    [VBA]Dim Xcell As Range
    For Each Xcell In Range("L8:R100")

    If Cells(Xcell.Row, 15).Value = "x" Then
    For i = 33 to 36 'Change to suit
    Cells(Xcell.Row, i).Interior.ColorIndex = 3
    Next i
    End If

    Next Xcell
    [/VBA]

    You can use resize property as:
    [VBA]Cells(Xcell, 33).Resize(, 4).Interior.ColorIndex = 3[/VBA]

    You can also use range as:
    [VBA]Range(Cells(Xcell, 33), Cells(Xcell, 36)).Interior.ColorIndex = 3[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim Xcell As Range
    For Each Xcell In Range("L8:R100")

    If Cells(Xcell.Row, 15).Value = "x" Then
    Cells(Xcell.Row, 33).Resize(1, 4).Interior.ColorIndex = 3
    End If
    Next Xcell
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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