+ Reply to Thread
Results 1 to 4 of 4

Datepart function return m = 12 for empty cells

  1. #1
    Registered User
    Join Date
    12-08-2011
    Location
    washington dc
    MS-Off Ver
    Excel 2010
    Posts
    1

    Question Datepart function return m = 12 for empty cells

    I have been running to problem with datapart function while debugging existing macros. The problems is datepart returns 12 for m when it reads an empty cell. So the scripts get into a never ending loop of reading empty cells and returning 12; eventually program hangs.

    Here is the simple script:
    Please Login or Register  to view this content.
    The data in column 2 of sheet1 is as follows:
    11/01/2011
    11/02/2011
    11/03/2011
    11/03/2011
    12/1/2011
    12/1/2011
    12/2/2011
    11/03/2011

    Any suggestions why datepart does not do what it is supposed to do? Please advise.
    Is this an application bug never reported?

    Thanks,
    Last edited by NBVC; 12-08-2011 at 11:03 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: Datepart function return m = 12 for empty cells

    Hi there.

    I know it's a work around, but you could try adding an IF statement in your loop:
    Please Login or Register  to view this content.
    Best regards, Rob.
    Last edited by rscsmith; 12-09-2011 at 12:08 AM. Reason: Re-read the OP - my answer wasn't the best!

  3. #3
    Forum Contributor wamp's Avatar
    Join Date
    11-10-2008
    Location
    Norway
    MS-Off Ver
    97, 2000 & 2003 (diff. workstations)
    Posts
    184

    Re: Datepart function return m = 12 for empty cells

    I'd set the For loop to only loop through cells with values
    Please Login or Register  to view this content.
    _______________________________________________
    Remember:

    Read the rules!

    Use code tags! Place [CODE] before the first line of code and [/CODE] after the last line of code.

    Mark your post [SOLVED] if it has been answered satisfactorily.
    To do this, click EDIT in your original post, click GO ADVANCED and set the PREFIX. It helps everybody!

    If you are approve (or disapprove) of any members response to your problem, please click the star icon underneath their post and comment. It matters.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Datepart function return m = 12 for empty cells

    if empty is evaluated as zero, datepart will return the month number that refers to that date (in Excel dates are numbers): 31-12-1899
    If you only want dates to be evaluated, please indicate that accordingly in the code:

    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)

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