+ Reply to Thread
Results 1 to 17 of 17

Macro to constantly color every other visible row

  1. #1
    Registered User
    Join Date
    01-19-2015
    Location
    Canton, Michigan
    MS-Off Ver
    2010
    Posts
    11

    Macro to constantly color every other visible row

    Hello all,

    I am new to the forum, and I apologize if this has been posted elsewhere. I have a file that I would like all visible rows in a certain range to alternate colors. For example, the first row is white, next is gray, etc. My range is A10:Q25. I have found a few macros and other methods for accomplishing this. The biggest issue that I am having is that the alternating colors don't update when I add, remove, or hide the rows within that range. Hiding seems to be the worst issue. Some macros seem to update when I add/remove rows, but hiding rows always gives me problems. The file is going to be used as a template, so rows will be changed regularly. Can someone suggest a macro that would work within A10:Q25 that would alternate row colors consistently and continuously, even if rows within that range are added, removed, or hidden?

    For reference, I have experimented with "format as table", but I need some cells to be merged, which apparently eliminates that option. I also have attempted some conditional formatting, but I haven't found an equation that updates consistently. I'm guessing that a macro would be the best option, but I'm open to anything that works. Honestly, having the row colors update as I hide/show the rows would be enough. Any help would be appreciated.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to constantly color every other visible row

    Hi, and welcome to the forum

    It would be best if you upload the workbook so that we have something to work with. You mention merged cells. Are these absolutely vital. Most of us avoid merged cells like the plague. They just cause too many problems with all sorts of functionality which we needn't rehearse here just at the moment.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    07-29-2014
    Location
    Oz
    MS-Off Ver
    2010
    Posts
    142

    Re: Macro to constantly color every other visible row

    This:
    Please Login or Register  to view this content.
    It only updates when you select a different cell after hiding/unhiding rows, but it works. Change the 17 if your table has more columns than A:Q

    Dan

    PS: your merged cells should not be more than one row, otherwise this may throw errors and/or wreck your table (also, I don't see how this table would work if your cells are merged for more than one row)
    Last edited by Danerida; 01-19-2015 at 07:11 PM.
    Don't forget to ☆ me if I helped you!

  4. #4
    Registered User
    Join Date
    01-19-2015
    Location
    Canton, Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Macro to constantly color every other visible row

    Hi Dan,

    Thank you for your reply. That is exactly what I needed. I knew there had to be some way to do this.

    Please forgive my inexperience with VBA, but I have a follow up question regarding your post. I noticed that the alternate row coloring starts at row 10, but continues infinitely down the sheet. Is there a way to alter the code to make the coloring fall within a certain range (or ranges)? Perhaps it starts at row 10 and ends at row 30, for example? And is it possible to have the coloring start up again after a few rows?

    I will see what I can do to eliminate the merged cells. I have only been merging cells along a row (horizontally) and never between rows (vertically). I don't know if that makes a difference or not.

    Thanks again, I appreciate your help.

  5. #5
    Forum Contributor
    Join Date
    07-29-2014
    Location
    Oz
    MS-Off Ver
    2010
    Posts
    142

    Re: Macro to constantly color every other visible row

    Change the resize section of this line to suit your needs:
    Please Login or Register  to view this content.
    I initially made it to find the last row of data assuming from your first post that you wanted a flexible table range. If you want it to be fixed, take out the "Cells(10,1).End(xlDown).Row - 9" and replace it with the number of rows that your table covers -1. i.e. if your range is A10:Q20, then use:
    Please Login or Register  to view this content.
    Horizontally merged cells will not effect this code.
    Dan

  6. #6
    Registered User
    Join Date
    01-19-2015
    Location
    Canton, Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Macro to constantly color every other visible row

    This worked perfectly. I noticed it seemed to process faster when the rows were limited, which was an added benefit. Thank you again for your assistance. Out of all of the ways that I have tried to alternate row colors, this seems to be the most reliable method.

  7. #7
    Forum Contributor
    Join Date
    07-29-2014
    Location
    Oz
    MS-Off Ver
    2010
    Posts
    142

    Re: Macro to constantly color every other visible row

    You're welcome.

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Macro to constantly color every other visible row

    I wouldn't use a macro, I'd use Conditional Formatting with the formula =(MOD(ROW(),2)=1)
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Forum Contributor
    Join Date
    07-29-2014
    Location
    Oz
    MS-Off Ver
    2010
    Posts
    142

    Re: Macro to constantly color every other visible row

    Quote Originally Posted by mikerickson View Post
    I wouldn't use a macro, I'd use Conditional Formatting with the formula =(MOD(ROW(),2)=1)
    That doesn't work or update as required by the OP when rows are hidden.

  10. #10
    Registered User
    Join Date
    01-19-2015
    Location
    Canton, Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Macro to constantly color every other visible row

    This code has been working very well for me so far. I am wondering if someone can help with one last hurdle I am experiencing. I would like to find a way to get this code to stop and start in different places as I move down the sheet. I have been experimenting with changing the range, but I think the complexity of the code is beyond my abilities. I would like the alternating row colors to start at a specific location (the first row under the header in "Table 2" in the attached image), and continue down to the last row of table 2. It would skip past the header of table 3, and start up again until it reached the bottom of table 3. The process would repeat for each table (4,5,6,etc.) until it got to the bottom of the last table.

    The main issue is that the number of rows in each table are not predetermined. People will be adding, deleting, and hiding rows in each table section. Sometimes the user may hide a whole table section completely (Table 3 for example).

    If anyone has any thoughts on how to make something like this work, I would greatly appreciate it.Excel Screen Example.JPG

  11. #11
    Registered User
    Join Date
    01-19-2015
    Location
    Canton, Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Macro to constantly color every other visible row

    I had thought about adding text to a column on the right (not in the print area) on each row that I would want skipped. It would be nice to have the code be able to skip over rows containing text in that column. Perhaps there would be an easier way though.

    Update: I used this method and it seems to be working well. I basically created a column off to the right (non printing area) and filled in text on all rows that I didn't want the alternating colors to apply to. For example, I would write "Dark Gray" on any row that I wanted to be dark gray. Then I updated the code so that the first thing it did was look for all rows with that text. It would apply that formatting first, and then format all blank rows with alternating row colors. I hope this helps anyone who is trying to create a similar table. Thank you all again for your feedback.
    Last edited by Starstopper31; 01-28-2015 at 03:59 PM.

  12. #12
    Registered User
    Join Date
    01-19-2015
    Location
    Canton, Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Macro to constantly color every other visible row

    Upon further review, the method I described in post #11 doesn't seem to work when I start to hide rows. I still need to find a way to stop and start the alternating row colors so that the sheet ends up looking like the attached image in post #10. The code as it is written (see attached, below) works really well, but I need to be able to tell it to skip rows that I am using for a header. It needs to be dynamic though, as the row numbers will change as they get added, subtracted, or hidden. I thought that adding a column off to the right with a variable would work best in identifying the rows to skip, but I am open to other suggestions.

    Please Login or Register  to view this content.
    I should mention that in column "Y" (represented by the #25 above), I put a value in the row that I wanted the alternating colored rows to stop. That way the code doesn't go all the way to the bottom of the page.
    Last edited by Starstopper31; 01-29-2015 at 09:22 AM.

  13. #13
    Forum Contributor
    Join Date
    07-29-2014
    Location
    Oz
    MS-Off Ver
    2010
    Posts
    142

    Re: Macro to constantly color every other visible row

    There may be a way to achieve this, but it seems to me that the built in "format as table" option is the best solution for your requirements. You can add, delete and hide rows and the formatting updates as you require. The limitation, of course, is your merged cells. You can still have the wider fields in your tables without merging those cells (they will just use multiple columns, but appear the same).

    You could try using named ranges for your table areas and then running a loop through each named range or something similar.
    Dan

  14. #14
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Macro to constantly color every other visible row

    If you have a column that always has entries, you could use Conditional Formatting with the formula

    =MOD(SUBTOTAL(103,$A$1:$A1),2)=1

    Which adjusts to hidden rows.

  15. #15
    Registered User
    Join Date
    01-19-2015
    Location
    Canton, Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Macro to constantly color every other visible row

    After some experimenting, I altered the formula that Dan originally suggested in post #3. I set aside columns to the right of the page (#26-39 in this case, all not in the print area) and designated a whole column for each stop and start point. I would put one line of text in each column on the row that I wanted the formatting to start and stop. Using the xlUp option, I would have the code identify the marked rows to set my range. The code was just repeated over and over with different ranges set. I added in text to deal with errors, in case entire ranges were hidden, it would just skip down to the next section of code. So far it seems to be working really well. It was probably more complicated than it needed to be, but I don't care as long as it works. Thank you all for your assistance. I have pasted my code below in case it might help others get started.

    Please Login or Register  to view this content.
    One unexpected quality that I didn't think about is that having the code automatically update eliminates my undo option. I'm thinking of changing the way that it works, maybe having the rows update each time I save instead of after every click. Since saving affects the whole workbook instead of just the current sheet, I'm assuming there will be more steps involved. It might be a topic for another day.

  16. #16
    Registered User
    Join Date
    02-27-2024
    Location
    USA
    MS-Off Ver
    365
    Posts
    1

    Re: Macro to constantly color every other visible row

    Hi Dan,

    This post is pretty old but it's very close to what I am trying to do. Same concept where the sheet will update the formatting to every other considering the rows that get hidden. However if I wanted the loop to keep looking until it found a blank row, how would that look? The code you provided has a specific number of rows, how could I make it so the loop counts and fixes formatting up until "empty row" instead of "row 10"

  17. #17
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Macro to constantly color every other visible row

    Quote Originally Posted by urmine190 View Post
    Hi Dan,

    This post is pretty old but it's very close to what I am trying to do. Same concept where the sheet will update the formatting to every other considering the rows that get hidden. However if I wanted the loop to keep looking until it found a blank row, how would that look? The code you provided has a specific number of rows, how could I make it so the loop counts and fixes formatting up until "empty row" instead of "row 10"
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #1 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ 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: 11-21-2014, 09:25 AM
  2. Constantly adding items to a working macro
    By lukeduggan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-22-2014, 07:34 AM
  3. Replies: 1
    Last Post: 10-19-2012, 08:54 PM
  4. [SOLVED] Looking for help tidying up Macro vba code / visible/non visible buttons
    By darrenkaye in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-03-2012, 11:50 AM
  5. macro with a pivot table where data is constantly added
    By mariusescu in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-10-2008, 02:38 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