+ Reply to Thread
Results 1 to 9 of 9

Coloring multiple rows

Hybrid View

  1. #1
    Registered User
    Join Date
    05-25-2010
    Location
    Copenhagen
    MS-Off Ver
    Excel 2003
    Posts
    2

    Coloring multiple rows

    Hello

    I have nearly the perfect macro for my job, but I need one little change. The thing I is that my macro should color the row within the search conditions is and the two rows below. At this moment it only colors the row with the search result.

    Heres my macro:

    Sub Farv_weekend()
    
        Dim LRow As Integer
        Dim LCell As String
        Dim LColorCells As String
        'Start at row 4
        LRow = 4
    
        'Update row colors for the first 150 rows
        While LRow < 150
            LCell = "A" & LRow
            'Color will changed in columns A to G
            LColorCells = "A" & LRow & ":" & "G" & LRow
    
            Select Case Left(Range(LCell).Value, 6)
    
                'Set row color to light blue
                Case "Lørdag"
                    Range(LColorCells).Interior.ColorIndex = 15
                    Range(LColorCells).Interior.Pattern = xlSolid
    
                'Set row color to light green
                Case "Søndag"
                    Rows(LRow & ":" & LRow).Select
                    Range(LColorCells).Interior.ColorIndex = 15
                    Range(LColorCells).Interior.Pattern = xlSolid
    
                'Default all other rows to no color
                Case Else
                    Rows(LRow & ":" & LRow).Select
                    Range(LColorCells).Interior.ColorIndex = xlNone
    
            End Select
    
            LRow = LRow + 1
        Wend
    
        Range("A1").Select
    
    End Sub
    Looking forward to get some help

    Anders
    Denmark
    Last edited by Anders.Hoejlund; 07-21-2010 at 07:37 AM.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Coloring multiple rows

    Sub snb()
        Range("A4:G150").Interior.ColorIndex = xlNone
        Range(Join(Filter([transpose(if(left(A4:A150,6)="Lørdag","A" & row(4:150)& ":G" & row(4:150),""))], "A"), ",")).Interior.ColorIndex = 15
        Range(Join(Filter([transpose(if(left(A4:A150,6)="Søndag","A" & row(4:150)& ":G" & row(4:150),""))], "A"), ",")).Interior.ColorIndex = 16
    End Sub

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Coloring multiple rows

    The smallest change I can think of would be to just add this:
            LColorCells = "A" & LRow & ":" & "G" & LRow + 2

    SNB, yours is intriguing but not working for me. I get error messages when I insert that into a module.
    Last edited by JBeaucaire; 07-19-2010 at 09:46 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Coloring multiple rows

    @JBeaucaire

    See the attachment.
    Attached Files Attached Files

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Coloring multiple rows

    Very intriguing, something new to pull apart.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Coloring multiple rows

    @JBeaucaire

    To provide you with another example, using the same techniques for quite another purpose, look here.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Coloring multiple rows

    Yeah, I'd seen that before. I have a rule against variables that give no indication what they do, code is hard enough to read without intentionally creating variables that make it significantly harder to decipher, I think we've talked about this briefly in the past.

    I know that means the code will be longer, but longer and legible wins for me from a teaching standpoint.

    The technique is pretty advanced as it is...

  8. #8
    Registered User
    Join Date
    05-25-2010
    Location
    Copenhagen
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Coloring multiple rows

    Thank you all very much, I inserted + 2 and deleted the part of the code wich gave default no color, this kindly deleted the coloring of the two rows below, just after they were colored. It works now.

    Have a nice day

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Coloring multiple rows

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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