Consulting

Results 1 to 6 of 6

Thread: Solved: Every other line

  1. #1
    VBAX Regular
    Joined
    Feb 2010
    Posts
    41
    Location

    Solved: Every other line

    I am trying to use the below macro to skip every other line and highlight it Grey. However, the range of data will be different every time. How can I get the below to loop so it will highlight all of the lines even if the range changes? Thanks for all of your help.

    [VBA]Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Interior.ColorIndex = 49
    Selection.Font.ColorIndex = 2

    Selection.Offset(2, 0).Select
    With Selection.Interior
    .ColorIndex = 15
    .Pattern = xlSolid
    End With[/VBA]

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    [VBA]
    Option Explicit
    Sub EveryOther()
    Dim i As Long
    With ActiveSheet.Cells(1, 1).CurrentRegion
    With .Rows(1)
    .Interior.ColorIndex = 49
    .Font.ColorIndex = 2
    End With

    For i = 3 To .Rows.Count Step 2
    With .Rows(i).Interior
    .ColorIndex = 15
    .Pattern = xlSolid
    End With
    Next i
    End With
    End Sub
    [/VBA]

    Paul

  3. #3
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    One way:
    [VBA]Sub alt()
    Dim i As Long, lRow As Long
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    If lRow Mod 2 = 0 Then lRow = lRow - 1
    For i = lRow To 3 Step -2
    With Range("a" & i & ":C" & i).Interior
    .ColorIndex = 15
    .Pattern = xlSolid
    End With
    Next
    End Sub[/VBA]

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    another:
    [VBA]
    Sub grey()
    Dim Rw As Range
    For Each Rw In ActiveSheet.UsedRange.Rows
    If Rw.Row > 2 And Rw.Row Mod 2 = 1 Then Rw(1).Resize(, 3).Interior.ColorIndex = 15
    Next
    End Sub

    [/VBA]
    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'

  5. #5
    VBAX Regular
    Joined
    Feb 2010
    Posts
    41
    Location
    Thanks for the responses!

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Note that .UsedRange might include currently empty rows below your block of data sometimes, usually if there was data there and you didn't specifically delete the now empty rows, or if you did not reset the last used row in VBA.

    Paul

Posting Permissions

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