+ Reply to Thread
Results 1 to 3 of 3

Error with Type Mismatch

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Question Error with Type Mismatch

    Hey there, I am trying to check a user input to see if the user has entered the date correctly.

    I am using the code below but it keeps giving me a "Type Mismatch" error. Does anyone know where I am going wrong?

    Sub remove_old_data()
    
    Dim dateDim As String
    
    Do While (date_check(dateDim) = 1)
    
        dateDim = InputBox(Prompt:="Please enter the date you wish to filter from: ", _
              Title:="Date Filter", Default:="Date filter")
    Loop
    
    End Sub
    
    
    Function date_check(datePar As String)
    
    If (Format(datePar) <> Format(CDate(datePar), "DD/MM/YYYY")) Then 'checks if the format of the cell is equivalent to "DD/MM/YYYY", as required for import
            MsgBox "Please enter the date in the format ""DD/MM/YYYY"".", vbInformation, "Remove Old Data"
            'highlights in which cell the error took place
            date_check = 1                      'returns a 1 to indicate an error has occured
            Exit Function                       'exits the function
        Else
            date_check = 0
        End If
        
    End Function
    Any help would be greatly appreciated, thanks.

    Jag
    Last edited by therealjag; 02-17-2010 at 10:30 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Error with Type Mismatch

    First time into the function the argument is blank,

    Try moving the test to the end of the loop

    Do
    
        dateDim = InputBox(Prompt:="Please enter the date you wish to filter from: ", _
              Title:="Date Filter", Default:="Date filter")
    Loop While (date_check(dateDim) = 1)
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Error with Type Mismatch

    Whenever a non-numeric string is passed to the date_check function the Cast to Date will generate a debug.

    Could you not allow any date format and simply format result per requirements, eg:

    Sub Remove_Old_Data2()
    Dim dateDim As Date, strDate As String
    dateDim = Application.InputBox(Prompt:="Please enter the date you wish to filter from: ", _
              Title:="Date Filter", Default:=Format(Date, "DD/MM/YYYY"), Type:=1)
    If dateDim Then
        strDate = Format(dateDim, "DD/MM/YYYY")
    Else
        MsgBox "Action Cancelled"
    End If
    End Sub

    It's not clear how the string equivalent of the Date is being used but the strDate is there to demo the principle.

+ 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