+ Reply to Thread
Results 1 to 10 of 10

Loops and Dates

  1. #1
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Loops and Dates

    Hi all, I am stumped with a problem involving a loop that searches back 1 week. As it is, my loop will search each cell looking for the date that is 7 days ago. It works fine until it finds that the date is not there. For example, 7 days ago was a Canadian holiday and therefore no update was made to my spreadsheet on the 1st of July. Being that, my loop skips right past and procedes to the top of the page and right into a runtime error. Basically, I need to implement a code that says: If the date does not exist, then stop on the day following (eg July 2). Here is my code:

    Please Login or Register  to view this content.
    I hope this is clear enough, please let me know if you need more.

    Cheers:

    Mordred
    Last edited by Mordred; 07-13-2010 at 05:22 PM. Reason: It is Solved

  2. #2
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Loops and Dates

    Hi please put your code in code tags.
    The answer you've asked for is easy, there is also a far better way, in general, of doing this. I can share both thoughts when it won't get me in trouble for not following the forum rules...

    CC


    If you feel really indebted please consider a donation to charity. My preferred charity is ActionAid but there are plenty of worthy alternatives.

  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loops and Dates

    Sorry about that, the tags are on.
    Last edited by Mordred; 07-08-2010 at 02:33 PM.

  4. #4
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Loops and Dates

    First:
    Please Login or Register  to view this content.
    can go to:
    Please Login or Register  to view this content.
    (which hasn't really helped much)
    when you say stop on the day following (e.g. 2nd July) - are your dates ascending or descending down the page?

    Like
    01 Jul
    02 Jul
    03 Jul
    or
    03 Jul
    02 Jul
    01 Jul
    ?

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loops and Dates

    My dates are ascending:
    June 30
    July 2
    July 3

    I can get the loop to stop and then add a row for the new date but I found that it will add the date even if it already exists. I'd show you the code but I lost it last night when my pc at home crashed.

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loops and Dates

    So this is where I'm at now. I can *** through the dates, insert a row where the date is missing, and then insert the date. However, I still have a problem with inserting a row and the date when the date is already present.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Loops and Dates

    The obvious thing, to me, therefore, seems to be:
    NOT stepping up through every cell until finding a match (your method)
    pseudocode:
    Please Login or Register  to view this content.
    (slightly needer way round)

    NOT looping a find on the range with a descending number until a match is found (a slightly better method worth mentioning for other situations)
    pseudocode:
    Please Login or Register  to view this content.
    (the date is irrelevant once the find 'hits')

    BUT simply using a built-in function, like lookup or match...
    pseudocode:
    Please Login or Register  to view this content.
    In your example:


    <-- SOrry, I went to the pub whilst half-way through writing this-->

    In your example:
    Please Login or Register  to view this content.
    should return the row number of the next oldest date behind your expected date
    ...
    or something liek that


    It's hard to get it right without an example, probably be able to put proper conetxt if you uploaded an example.

    hth

  8. #8
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Smile Re: Loops and Dates

    Thanks Cheeky, I'm going to try what you suggested. I would upload the code but I have been playing around with it so much over the last couple of days that it is a mess right now. I'll have to spend some time to clean it all up first when I get back to work after the weekend.

  9. #9
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loops and Dates

    Hi Cheeky, I had a hard time trying to implement your code and I kept getting errors in the process. So, I went back to my loops and finally, I solved it. Here is what I did:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I've tested this and retested this and it works well. However, if more than one day is missing, eg: July 1 and July 2, a runtime error is thrown. For my purposes though, I only have to worry about one missing day so that doesn't really matter.

    Anyways, thank you for the energies you put into trying to help me Cheeky, I do appreciate it.
    Last edited by Mordred; 07-13-2010 at 05:23 PM. Reason: It is Solved

  10. #10
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Loops and Dates



    That's fine for you.
    If anyone finds this thread, don't employ the solution provided. If you don't understand someone's advice explain why & where (specifically) and upload an example when asked.

+ 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