Results 1 to 3 of 3

Thread: Excel VBA custom class - argument validation

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Right here

    Excel VBA custom class - argument validation

    I have the following custom class:

    Private m_DateTime As Date
    Public Property Let DateTime(ByVal pDateTime As Date)
    'allow the date & time to be set
    'first check to see if argument being passed is actually in date format
        If IsDate(pDateTime) Then
             m_DateTime = pDateTime
            MsgBox ("Date format incorrect")
        End If
    End Property
    The idea was to use the If...Then...Else statement to check to see if the value passed by the user (pDateTime) is actually in date format. However when I tried to test to validation by passing a string to the Let DateTime procedure I get "Run-time error '13': Type mismatch".

    I'm not sure what I'm doing wrong though...anyone offer any advice on how they'd do this?

    Last edited by TheRobster; Jan 8th, 2015 at 04:29 PM.

  2. #2
    Join Date
    Dec 2004

    Re: Excel VBA custom class - argument validation

    is the argument passed to the datetime procedure defined as a date? or a literal value?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed

  3. #3
    Fanatic Member
    Join Date
    Feb 2013

    Re: Excel VBA custom class - argument validation

    your going to get a type error if the data passed is not a date type if im not mistaken, it would be better to declare it as a variable instead of a date or handle it before it gets to your class, once you have the data there are several checks you can do to determine if its a date, ill list a couple of built in excel functions.

    string check, use the DateValue() function, it will return an excel date if format correctly (i think it only accepts US format mm/dd/yyyy or similar variants)
    you can use Month(), Day(), Year() functions on an excel format date to return the particular number
    and then use you can use i think Date(year,month,day) to return a string format date

    with this circle you can test using each function to indentify if a date is a date of some format

    use on error resume next and check if if the assigned variable has received anything, if its empty an error likely occured and that function cannot handle the data which means its not a date of that format.

    ill let you play, hope this helps
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width