+ Reply to Thread
Results 1 to 7 of 7

ISDATE function in VBA evaluating to false and i dont know why

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    ISDATE function in VBA evaluating to false and i dont know why

    Hi, i have a macro where i use the vba function ISDATE.

    I pass the isdate function two date formats.........but for some reason both evaluate to "false" and i am not sure why.

    i am enclosing small macro to show the problem i am having........

    Any ideas?
    Attached Files Attached Files

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: ISDATE function in VBA evaluating to false and i dont know why

    Try declaring the variables as dates

    Sub TEST()
    
    Dim fl_macro As String
    Dim userdate As Date
    Dim userdate2 As Date
    
    fl_macro = ThisWorkbook.Name
    
    Set ws = Workbooks(fl_macro).Worksheets(1)
    
    
    userdate = ws.Range("A1").Value2        'note...A1 is a date 1/12/12
    
    userdate2 = 40920
    
    
    
    If IsDate(userdate) Then
        MsgBox ("user date is a date")
        
    ElseIf IsDate(userdate2) Then
        MsgBox ("userdate2 is a date")
        
    ElseIf Not IsDate(userdate) Then
        MsgBox ("userdate is NOT a date ")
        
    ElseIf Not IsDate(userdate2) Then
        MsgBox ("userdate2 is NOT a date")
    
    End If
    
    End Sub
    Martin

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: ISDATE function in VBA evaluating to false and i dont know why

    Hi,

    you are using the Value2 property and it returns a Double data type if the original data in the range is Date. Try using the Value property.
    http://msdn.microsoft.com/en-us/libr...ice.11%29.aspx

    Note: you didn't declare some of your variables, not sure if that's intentional.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: ISDATE function in VBA evaluating to false and i dont know why

    If you change this line

    userdate = ws.Range("A1").Value2
    INTO
    userdate = ws.Range("A1").Value

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    Re: ISDATE function in VBA evaluating to false and i dont know why

    thanks....i will take a look at your suggestions

    during the time i posted this question........the server went dead for this web site...........during that time i was thinking about what to do.......

    i came up with this idea of a validDate function.........it seems to work......i am going to include it below.......but it figures that i would go to a "long" soln when (looking at the suggestions thus far) when the real soln might be very simple.

    Function ValidDate(date_test As Variant)
    
        On Error GoTo yes_error
        
        
        If date_test = Null Or Len(date_test) = 0 Then
            ValidDate = False
            Exit Function
        End If
        
        
        If IsDate(CDate(date_test)) Then
            ValidDate = True
            GoTo no_error
        Else
            ValidDate = False
            GoTo no_error
        End If
        
        
        
    no_error:
        On Error GoTo 0
        Exit Function
        
        
    yes_error:
        ValidDate = False
        On Error GoTo 0
        Exit Function
    
    
    End Function

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    Re: ISDATE function in VBA evaluating to false and i dont know why

    this is not the first time i have run across the ".value" vs ".value2" issue.

    Any suggestions on when to use one vs the other? I read something somewhere that i should always use the ".value2" which is why i use it all the time.......but from time to time when working with dates i have an issue but not all the time.

    any suggestions/advice?

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: ISDATE function in VBA evaluating to false and i dont know why

    if you need dates to be treated as dates (and currency to be treated as such) use value otherwise use value2. the latter is often faster and can be safer if you may run into conversion issues such as date formatting-which is only an issue with non US formats and text representations of dates
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ 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