+ Reply to Thread
Results 1 to 9 of 9

Programmatically shade alternating lines in VBA macro ?

  1. #1
    Registered User
    Join Date
    11-05-2012
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    13

    Programmatically shade alternating lines in VBA macro ?

    I would like to programmatically (in VBA macro) shade alternating lines
    of a section of a worksheet.

    I've done this manually, but can't find documentation on how to do it via VBA.

    The section of the worksheet I want to alter begins on line 11, and goes a
    varying number of lines down.

    So, I imagine I'd do something like I did here to sort the data on column M ...

    Worksheets("Out").Range("A11").End(xlDown).Sort _
    Key1:=Worksheets("Out").Range("M11"), _
    order1:=xlAscending


    Any help would be appreciated. Thanks

  2. #2
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    Re: Programmatically shade alternating lines in VBA macro ?

    It would be much quicker and easier to use conditional formatting (se Chandoo.org and 'tiger stripes'), however the customer is often correct

    I don't understand the code shown; it sorts, not colours.
    Please Login or Register  to view this content.
    does it.
    Last edited by brynbaker; 11-06-2012 at 01:48 PM. Reason: code tested and corrected

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Programmatically shade alternating lines in VBA macro ?

    Hi Rex 99,

    Something like this (you choose the color index):

    Please Login or Register  to view this content.
    Put that code in a module - then

    Please Login or Register  to view this content.
    Call it after your sort.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Registered User
    Join Date
    11-05-2012
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Programmatically shade alternating lines in VBA macro ?

    Thanks,


    I tried both methods above - and both only shaded column A. I want multiple colums shaded - entire rows.

    Also ... is there a command to set the active sheet, or better yet, a means to specify what sheet I want to work with - so I don't have to change the active sheet. Hope that made sense.

    Thanks

  5. #5
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    Re: Programmatically shade alternating lines in VBA macro ?

    Please Login or Register  to view this content.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Programmatically shade alternating lines in VBA macro ?

    Sorry 'bout that:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-05-2012
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Programmatically shade alternating lines in VBA macro ?

    Brynbaker ... Rows(r).Select


    Thanks but this gives Run Time error '1004' Application defined or object defined error. Running off to work and haven't had time to google that.
    It apparently works at selecting the sheet I specify.


    Xladept ..... works, but wrong sheet. Why doesn't it work on sheet of

    sheets("your sheet").activate


    ???
    Last edited by rex99; 11-07-2012 at 12:52 PM.

  8. #8
    Registered User
    Join Date
    11-05-2012
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Programmatically shade alternating lines in VBA macro ?

    OK ... got it .... This worked.


    Sub Shadem(): Dim i As Long
    Sheets("Out").Activate
    For i = 11 To ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row Step 2
    ActiveSheet.Range("A" & i).EntireRow.Interior.ColorIndex = 6
    Next i
    End Sub



    Thank you all

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Programmatically shade alternating lines in VBA macro ?

    Or this:

    Please Login or Register  to view this content.
    And you're welcome!

    P.S. let the the color index be xlNone to restore.
    Last edited by xladept; 11-07-2012 at 01:23 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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