+ Reply to Thread
Results 1 to 9 of 9

How do I store the current date to a variable and compare it to cells that have dates?

  1. #1
    Registered User
    Join Date
    11-06-2012
    Location
    ndemene
    MS-Off Ver
    Excel 2010
    Posts
    74

    How do I store the current date to a variable and compare it to cells that have dates?

    Here is the current macro I am working with.:
    Please Login or Register  to view this content.
    I am trying to change an Excel file from the bottom-up until I find a date that is not today's date (where today means the day you look at the file and not the day this forum post is posted) in which case, I just want the copying process to stop but I am not able to successfully compare the dates.

    The compiler says:
    Run-time error '1004':

    Application-defined or object-defined error
    and suggests that the following line is at fault.:
    Please Login or Register  to view this content.
    Any help in getting the loop and date comparison to work well would be greatly appreciated!

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do I store the current date to a variable and compare it to cells that have dates?

    Try:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    11-06-2012
    Location
    ndemene
    MS-Off Ver
    Excel 2010
    Posts
    74

    Re: How do I store the current date to a variable and compare it to cells that have dates?

    Thanks for your answer. I changed row to currentRow and that did not fix it. I did some more checking to make sure and the Do Until loop works fine. It seems that the issue is the comparison of dates.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: How do I store the current date to a variable and compare it to cells that have dates?

    You are formatting a Date value into a text string then assigning it back to a Date variable. I don't think that will cause a runtime error, but it's unnecessary and caused an error in my code because I'm configured for US dates and your format returns a European date. Try this.

    Please Login or Register  to view this content.
    Actually, you don't need the variable at all; you can just use Date anywhere you are using todayDate. I ran code very similar to this and had no runtime error on the comparison:

    Please Login or Register  to view this content.
    By the way, the issue you fixed above (row -> currentRow) is the reason that you should always use "Option Explicit" (Tools, Options, Require variable declarations).
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Registered User
    Join Date
    11-06-2012
    Location
    ndemene
    MS-Off Ver
    Excel 2010
    Posts
    74

    Re: How do I store the current date to a variable and compare it to cells that have dates?

    Thanks for the tip; I now checked the "Require variable declarations" box.

    Also, I tried what you said and, it did not cause any runtime errors or anything but, it just did nothing. This leads me to believe that the date formatting may be an issue since I'm Canadian and my software may be either set up for the anglophone part of Canada or the US and I think the system I'm using is the French system (from the francophone part of Canada) for this Excel file.

    Would you be able to help me with that?

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: How do I store the current date to a variable and compare it to cells that have dates?

    I"m not knowledgeable about switching around localization settings in Excel (e.g., I don't know what is configuration specific to a file vs. specific to the installation of Excel), but I would be happy to at least take a look if you can attach your file.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do I store the current date to a variable and compare it to cells that have dates?

    Try:

    Please Login or Register  to view this content.

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: How do I store the current date to a variable and compare it to cells that have dates?

    If xladept's solution works it means that the data in your worksheet is stored as strings instead of dates.

  9. #9
    Registered User
    Join Date
    11-06-2012
    Location
    ndemene
    MS-Off Ver
    Excel 2010
    Posts
    74

    Re: How do I store the current date to a variable and compare it to cells that have dates?

    Sorry for my late response but, the format was actually right all along. It didn't work because the test dates I put were wrong.

    Is it running correctly just because my computer is likely set to "francophone Canadian details"? (I'm an anglophone but, I live in a francophone place and I have my settings set accordingly - in other words, my operating system knows I live in a french city but I chose American English as the language the OS uses to interact with me.) Is it possible that my VBA program will run in "American" or "Canadian anglophone" (or any other) mode if run on another computer with the exact same syntax?

+ 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