+ Reply to Thread
Results 1 to 13 of 13

Looking for a faster way to change the background color of a bunch of cells

  1. #1
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Looking for a faster way to change the background color of a bunch of cells

    Greetings,

    I have a spreadsheet which contains a few hundred (or more) colored cells.

    Within my spreadsheet I have an different tab, called my User_Options tab, that allows the user to specify the exact color and shade of these cells. Once they do, and once they switch back to the main tab, all of the cells with this particular background color change to the color they specified.

    It's working fine... no problems... but it's slowwwwww.

    I'm using a simple, double FOR NEXT loop. For example:

    Please Login or Register  to view this content.
    Is there a way to recode this, so that it's much faster? The cells that contain the "x" that need to be changed are all over the place, and in no order. If I knew ahead of time that I would be changing every cell in a particular row, I could select the entire row all at one, and change it all in one shot. But I don't know that.

    Thanks in advance.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Looking for a faster way to change the background color of a bunch of cells

    What other values are there if they are not "x"?
    多么想要告诉你 我好喜欢你

  3. #3
    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,197

    Re: Looking for a faster way to change the background color of a bunch of cells

    Hi, Ed_Collins,

    try a For Each on the whole range in geting it all done in one Loop if you want to change each individual cell or make use of WorksheetFunction.CountIf to see if any row/column meets the criteria. Or utilize Find to set an object to the cell and work with it.

    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

  4. #4
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: Looking for a faster way to change the background color of a bunch of cells

    Quote Originally Posted by millz View Post
    What other values are there if they are not "x"?
    They aren't even "x". That's just an example.

    The cells are actually one of four different values. Let's call the values "w", "x", "y" and "z". The user can change the background colors of all of these cells, to suit their own personal taste, if they desire.

    The FOR NEXT loop works... it looks at every cell and if the background color needs changing, it does it. But the program noticeably slows down as it goes through this FOR NEXT loop and changes the cell colors. (And yes, ScreenUpdating is false.)
    Last edited by Ed_Collins; 09-17-2014 at 12:23 AM.

  5. #5
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: Looking for a faster way to change the background color of a bunch of cells

    Quote Originally Posted by HaHoBe View Post
    try a For Each on the whole range in geting it all done in one Loop if you want to change each individual cell or make use of WorksheetFunction.CountIf to see if any row/column meets the criteria. Or utilize Find to set an object to the cell and work with it.
    Yea, that's the problem. I don't know how to code that. I don't know how to check the whole range and only change the cells needed. I only know how to check each cell individually, as shown.

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Looking for a faster way to change the background color of a bunch of cells

    Try something like this? where "x" is one of the four values. I am assuming you don't have any blank cells within the range.
    Please Login or Register  to view this content.

  7. #7
    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,197

    Re: Looking for a faster way to change the background color of a bunch of cells

    Hi, Ed_collins,

    assuming htat the first row holds the header and Columnn A is filled throughout:
    Please Login or Register  to view this content.
    @millz:

    you´re sure there are no empty cells in the range before your macro and that the value will not be needed later on as well?

    Ciao,
    Holger

  8. #8
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Looking for a faster way to change the background color of a bunch of cells

    Quote Originally Posted by HaHoBe View Post
    @millz:

    you´re sure there are no empty cells in the range before your macro and that the value will not be needed later on as well?
    Hi HaHoBe, I don't quite understand what you mean.

    Having blanks or not was what I first asked OP, and OP said there's 4 possible values. Also, I did not assume the value will not be needed later. And your code is highlighting the entire row within the range if any "x" exists within that row. OP only wants to highlight individual cells I think.



    @Ed_Collins, if your data consists of blanks and the 4 possible values, you can try this:
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: Looking for a faster way to change the background color of a bunch of cells

    milz, in my informal timing test, your code runs in less than half the time as my code. Thanks. That's a good start and might be all I need. (I figured there must be some way to select the entire range, but I didn't know how.) I will play around with this for a bit and experiment some more. Thank you for taking the time to respond.

    HaHoBe, I will also test your code and see if it works (which I'm sure it does) and to see how much faster it is, compared to my code and to that of milz. Thanks!!

  10. #10
    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,197

    Re: Looking for a faster way to change the background color of a bunch of cells

    Hi, Ed_collins,

    you woiuld need to excahnge the equation fopr the worksheetFunction to meet the number of used cells in that row/column and may skip to the next row/column.

    ...in less than half the time
    It should be a lot faster. You might consider to turn Calculation to Manual at the begin and turn in back to Automatic after the code.

    Ciao,
    Holger

  11. #11
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Looking for a faster way to change the background color of a bunch of cells

    This might speed up a small fraction more.
    Please Login or Register  to view this content.
    Edit: And also consider the suggestion by HaHoBe to switch off/on calculation.
    Last edited by millz; 09-17-2014 at 01:37 AM.

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Looking for a faster way to change the background color of a bunch of cells

    If all the cells have the same formatting, why not use a Style then all you have to do is modify the Style properties.
    Remember what the dormouse said
    Feed your head

  13. #13
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Looking for a faster way to change the background color of a bunch of cells

    hi Ed,
    How does this one work for you?
    Blanks no matter. Styles no matter.
    Please Login or Register  to view this content.

+ 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. Change cell background color based on another cells background color
    By Queo in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 4
    Last Post: 06-10-2014, 05:28 AM
  2. [SOLVED] change matched cells background color
    By nawGo in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-13-2014, 04:08 PM
  3. [SOLVED] Macro to change all cells with a certain background color in another background color
    By kevinvzandvoort in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2012, 11:04 AM
  4. Replies: 1
    Last Post: 10-08-2010, 01:04 AM
  5. [SOLVED] Need to loop thru cells and change background color
    By Ian Wolstenholme in forum Excel General
    Replies: 6
    Last Post: 02-09-2005, 05:06 AM

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