+ Reply to Thread
Results 1 to 6 of 6

Making a cell change colors with every one second increment

  1. #1
    Forum Contributor
    Join Date
    05-17-2017
    Location
    Tallahassee
    MS-Off Ver
    15.32
    Posts
    206

    Making a cell change colors with every one second increment

    Hi everyone! I though I was getting pretty clever with the following code but it did not work at all lol. I am trying to get a range to change from one color to another, every second, for a minute. My formula, theoretically, should change from 1 to 0 every second (it is dividing the second by 2 and seeing if there is a decimal place). Thus, I can't get why it isn't working. Any help would be insanely appreciated!!!

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    12-17-2016
    Location
    South Wales
    MS-Off Ver
    Excel 2007, 2016
    Posts
    68

    Re: Making a cell change colors with every one second increment

    Some potential problems I can see:
    Please Login or Register  to view this content.
    This can never be true, as "now" will always be "now"! So the loop should run forever.
    Perhaps what you're intending would be more like:
    Please Login or Register  to view this content.
    However, assuming that your macro isn't changing the colours at all (rather than just not stopping after a minute), there's clearly something else wrong...

    Macro code takes time to execute. Not much time, but your method relies on it executing the following line exactly on a round second:
    Please Login or Register  to view this content.
    If its a fraction of a millisecond either way, the colour won't change!

    I think your best option is with nested loops, and checking whether "now" exceeds a calculated time (as in my suggested code above) rather than matching it exactly. For example:
    Please Login or Register  to view this content.
    There will always be the issue of what happens if the colour change code takes more than a second to execute.
    With my code above, the process will still stop after a minute - if the code runs slowly, you won't get the full 60 changes. At best you're only likely to get 59, as the 60th would be fractionally outside the one minute. If a full 60 changes is more important than the total run time, suggest that you change the outer loop to one woth a basic counter so that it just runs 60 times.
    Last edited by Trevor_S; 04-19-2019 at 01:21 PM.

  3. #3
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,384

    Re: Making a cell change colors with every one second increment

    .
    Here is an answer to your goal :

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    05-17-2017
    Location
    Tallahassee
    MS-Off Ver
    15.32
    Posts
    206

    Re: Making a cell change colors with every one second increment

    Quote Originally Posted by Trevor_S View Post
    Some potential problems I can see:
    Please Login or Register  to view this content.
    This can never be true, as "now" will always be "now"! So the loop should run forever.
    Perhaps what you're intending would be more like:
    Please Login or Register  to view this content.
    However, assuming that your macro isn't changing the colours at all (rather than just not stopping after a minute), there's clearly something else wrong...

    Macro code takes time to execute. Not much time, but your method relies on it executing the following line exactly on a round second:
    Please Login or Register  to view this content.
    If its a fraction of a millisecond either way, the colour won't change!

    I think your best option is with nested loops, and checking whether "now" exceeds a calculated time (as in my suggested code above) rather than matching it exactly. For example:
    Please Login or Register  to view this content.
    There will always be the issue of what happens if the colour change code takes more than a second to execute.
    With my code above, the process will still stop after a minute - if the code runs slowly, you won't get the full 60 changes. At best you're only likely to get 59, as the 60th would be fractionally outside the one minute. If a full 60 changes is more important than the total run time, suggest that you change the outer loop to one woth a basic counter so that it just runs 60 times.
    I'm going to test this and get back to you :D thanks so much! I may have a few questions about the explanation as well.

  5. #5
    Forum Contributor
    Join Date
    05-17-2017
    Location
    Tallahassee
    MS-Off Ver
    15.32
    Posts
    206

    Re: Making a cell change colors with every one second increment

    Quote Originally Posted by Logit View Post
    .
    Here is an answer to your goal :

    Please Login or Register  to view this content.
    I appreciate it! ) Is there any way you could explain the logic behind the code? I really want to learn VBA and I hate taking someone else's code if I can't explain it because then I feel I learned nothing lol.

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,384

    Re: Making a cell change colors with every one second increment

    .
    Here is my understanding of what the code means. If anyone sees an error please correct. Thanks.

    It will be easier to read if you copy the code below and paste it into the VBE for reading.

    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. [SOLVED] VBA code to change Excel chart bar colors as per cell colors
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-21-2016, 03:29 AM
  2. Replies: 3
    Last Post: 10-21-2015, 03:51 PM
  3. Worksheet On Change Calls Sub to change Cell colors
    By sportsguy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2014, 12:20 PM
  4. Change cell colors
    By Beastwood in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-29-2012, 02:08 AM
  5. Excel 2007 : How Do I Change Cell Colors??
    By Ranger SVO in forum Excel General
    Replies: 2
    Last Post: 05-25-2011, 10:31 PM
  6. Matching Cell to change colors?
    By codyaaron2 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-17-2009, 08:12 AM
  7. [SOLVED] making copied cells change with change in original cell
    By Jennifer Mcdermeit in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-20-2006, 11:58 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