+ Reply to Thread
Results 1 to 12 of 12

Highlighting every other row a different color with vba

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-28-2013
    Location
    Green Bay, WI
    MS-Off Ver
    Excel 2007
    Posts
    125

    Highlighting every other row a different color with vba

    Hello,

    I have a working macro to highlight whatever I select one color. I need it to be able to color every other row in my selection.....Sometimes I just select a grid and sometimes I select entire columns. I would then like to select a second color to start at the second row of my selection.

    Any help would be greatly appreciated.

    Thank you.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Highlighting every other row a different color with vba

    Hi, Spreadlover,

    itīs clear to you that any exisiting colour should be saved and restored later on?

    Sub EF956049()
    Dim lngCounter As Long
    Dim rngRow As Range
    Const clngFIRST As Long = 42
    Const clngSECOND As Long = 6
    
    For lngCounter = 1 To Selection.Rows.Count
      Select Case lngCounter Mod 2
        Case 0
          Selection.Rows(lngCounter).Interior.ColorIndex = clngSECOND
        Case 1
          Selection.Rows(lngCounter).Interior.ColorIndex = clngFIRST
      End Select
    Next lngCounter
    End Sub
    I have a working macro to highlight whatever I select one color.
    Maybe you should have enclosed that macro here.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    06-28-2013
    Location
    Green Bay, WI
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Highlighting every other row a different color with vba

    I apologize, this is the book I was working with. I see I did not post it.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    06-28-2013
    Location
    Green Bay, WI
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Highlighting every other row a different color with vba

    This was the code I had.

     Sub HiglightandBorders()
    '
    '   Macro1 Macro
    '
    
    '   Add Highlight
    
        
        
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent3
            .TintAndShade = 0.599993896298105
            .PatternTintAndShade = 0
        End With
    
    '   Add Borders
        
        
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
    End Sub

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Highlighting every other row a different color with vba

    Hi, Spreadlover,

    you could replace the code lines
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent3
            .TintAndShade = 0.599993896298105
            .PatternTintAndShade = 0
        End With
    with
    Dim lngCounter As Long
    Dim rngRow As Range
    Const clngFIRST As Long = 42
    Const clngSECOND As Long = 6
    
    For lngCounter = 1 To Selection.Rows.Count
      Select Case lngCounter Mod 2
        Case 0
          Selection.Rows(lngCounter).Interior.ColorIndex = clngSECOND
        Case 1
          Selection.Rows(lngCounter).Interior.ColorIndex = clngFIRST
      End Select
    Next lngCounter
    and leave the rest of the code as it is.

    Ciao,
    Holger

  6. #6
    Forum Contributor
    Join Date
    06-28-2013
    Location
    Green Bay, WI
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Highlighting every other row a different color with vba

    Thank you for your response Holger. I apologize for the delay in my response. I haven't had a chance to take a peek at this until just now. The code seems to work properly on every test, except when I select columns A thru H......It stops excel from working and seems to crash it.

    Is there a way to speed up the calculations in the coding to keep excel from crashing or should I just try to make smaller selections at a time?

    Thanks,

    Spreadlover.
    Last edited by Spreadlover; 09-21-2013 at 05:17 PM.

  7. #7
    Forum Contributor
    Join Date
    06-28-2013
    Location
    Green Bay, WI
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Highlighting every other row a different color with vba

    Please disregard the last question. I see that when I stopped the code it had highlighted over 1,000,000 rows already. That is far more then I will ever need. I will just have to be a little more selective on choice or I will put a counter on it to stop at 5,000. Thank you again.

  8. #8
    Forum Contributor
    Join Date
    06-28-2013
    Location
    Green Bay, WI
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Highlighting every other row a different color with vba

    I have a question on whether or not I can easily use the rgb scale on color for this code. I have tried playing with it, but I have had no luck. The colors I am using on my spreadsheet are not showing up on the single number scale.

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Highlighting every other row a different color with vba

    Hi, Spreadlover,

    adjust the colours as wanted:
    Dim lngCounter As Long
    
    For lngCounter = 1 To Selection.Rows.Count
      Select Case lngCounter Mod 2
        Case 0
          Selection.Rows(lngCounter).Interior.Color = RGB(0, 80, 207)
        Case 1
          Selection.Rows(lngCounter).Interior.Color = RGB(255, 100, 87)
      End Select
    Next lngCounter
    Ciao,
    Holger

  10. #10
    Forum Contributor
    Join Date
    06-28-2013
    Location
    Green Bay, WI
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Highlighting every other row a different color with vba

    This works perfectly and it is very easy to match the color scheme. Each page has a bit of a varying color scheme, so the same macro with different names and a slightly different color code can be easily used to keep each sheet easily updated as new rows are added. Every time a new product is added a new row needs to be added and this works perfectly. I wanted to stay away from conditional formatting in order to have certain columns stay a specific color as well.

  11. #11
    Forum Contributor
    Join Date
    06-28-2013
    Location
    Green Bay, WI
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Highlighting every other row a different color with vba

    The original code seen above was written and works fine in Excel 2007 and up I believe, but I had trouble with the code on an older version at work running Excel 2003. I had to change the code to the following in order for it to give me the same results. This is just an update and a reference for anyone who might be looking for something similar.

    '   Add Highlight every other row
    
        
        
        Dim lngCounter As Long
    
        For lngCounter = 1 To Selection.Rows.Count
            Select Case lngCounter Mod 2
                Case 0
            Selection.Rows(lngCounter).Interior.Color = RGB(155, 204, 255)
                Case 1
            Selection.Rows(lngCounter).Interior.Color = RGB(0, 255, 0)
        End Select
        
        Next lngCounter
          
        
    '   Add Borders
        
        
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlThin
        End With
    End Sub

  12. #12
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    810

    Re: Highlighting every other row a different color with vba

    I like your code and I also want to add the below lines and put code in WorkSheet so I don't have to run a Macro:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Not Target Is Nothing Then
    ......your code here
    Regards,
    tt3

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 02-05-2013, 07:51 AM
  2. Replies: 6
    Last Post: 10-01-2012, 10:52 AM
  3. Replies: 1
    Last Post: 05-19-2010, 12:41 PM
  4. Replies: 5
    Last Post: 02-19-2010, 10:51 PM
  5. Comparing two values and highlighting using color
    By ramki in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-15-2006, 12:19 PM

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