+ Reply to Thread
Results 1 to 11 of 11

Macro to colour alternating rows

  1. #1
    Registered User
    Join Date
    04-06-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Macro to colour alternating rows

    Hi,

    I was wondering, what would the macro be to colour alternating rows.

    So I could select the range of rows, hit the macro - and it would colour them for me?

    I need it to do alternating green / lightgreen. For one, but I need to do other colours afterwards as well. So what would the code be to do this?

    Thanks
    -Rudey
    Last edited by Rudey; 04-15-2009 at 01:48 AM. Reason: solved =]

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Macro to colour alternating rows

    Hi Rudey,

    In Excel 2007 you have a few options without resorting to a macro.

    1. Use a Table style. Select your table of data, then in the Home ribbon click 'Format as Table' and either select a table style from the ones shown, or create your own by selecting 'New Table Style'

    2. Use Conditional Formatting (in 2007 you can have more than 3, so you can have 4, 5, 6 or more alternating colors) using the MOD function. For example:

    To highlight rows 1, 4, 7, 10, etc. with green, select your table of data and click Conditional Formatting on the Home ribbon. Select 'New Rule'. Select 'Use a formula to determine which cells to format', then in the formula textbox provided below it, type:
    =MOD(ROW(),3)=1

    Then click the Format button and select a Green fill color.

    To highlight rows 2, 5, 8, 11, etc., add another New Rule, but the formula this time should be:
    =MOD(ROW(),3)=2

    For the third set of rows (3, 6, 9, 12, etc.) use:
    =MOD(ROW(),3)=0

    If you still need a macro, let us know.

  3. #3
    Registered User
    Join Date
    04-06-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Macro to colour alternating rows

    Hey,

    Yeah I tried that out. But I'm using conditional formatting in my spreadsheet. And I need to do this in alot of places. So it just creates way ot many rules for me to sort though.

    I was also wanting to learn alittle about macros in order to do this.

    Thanks!

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Macro to colour alternating rows

    The macro *could* look something like this (for three alternating colors):
    Please Login or Register  to view this content.
    To find the right code for the color you want, you can search online for a list or palette of Excel 2007 colors, or simply record a macro while you set a cell's fill color to the color you want and review the code generated.

    Notice the two different methods of coloring used - one for "standard" colors and other for "theme" colors. When you record the macro for the color you want, you'll see which type you need to use.

    Hopefully that helps.

  5. #5
    Registered User
    Join Date
    04-06-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Macro to colour alternating rows

    Hi thanks for that. I get an error at:

    Please Login or Register  to view this content.
    When I run it though

  6. #6
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Macro to colour alternating rows

    Where did you put the code? It should be in one of the Worksheet's code windows, not a module. If necessary, add a Sheet name before UsedRange, e.g.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    04-06-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Macro to colour alternating rows

    Sorry I'm confused. What's meant by a code window instead of a module?

  8. #8
    Registered User
    Join Date
    04-06-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Macro to colour alternating rows

    Ah awesome. Made it run. BUt how would I make it only apply this formatting ONLY to the area I select. So if I select A1:A10 it would only apply that formatting to those cells.

    Also, thanks for teaching me about Cases I was looking for a good example of when to use them!

  9. #9
    Registered User
    Join Date
    04-06-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Macro to colour alternating rows

    Would anyone know the code to limit the colouring of the code to the area I select only?

    Edit: I came up with some code, but it has an error that is: Next without For. Could someone please help me with it, because I have never programmed VBA before.

    Please Login or Register  to view this content.
    Thanks
    Last edited by Rudey; 04-14-2009 at 08:36 PM. Reason: Added my attempt at solving the problem

  10. #10
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Macro to colour alternating rows

    Hi Rudey, try this updated code (changes in red):
    Please Login or Register  to view this content.
    Hope that helps.

  11. #11
    Registered User
    Join Date
    04-06-2009
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Macro to colour alternating rows

    Works! Thanks heaps!

+ 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