+ Reply to Thread
Results 1 to 27 of 27

Date and data validation not working

  1. #1
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Date and data validation not working

    Hello all,
    I'm trying to add code to validate the format in which users are entering in a date within my userform, and I am also trying to validate if they enter in a specific value within a combobox, they will be unable to enter data within a textbox further down in the userform. Here is how my code is currently written, but neither my date or data validations are working. Any help is appreciated as I'm in a bit of a time crunch to get this to work. I have bolded the new coding I added to an existing code that was created by someone else at my job prior to it being given to me. Thank you all!

    [code]
    Private Sub cmdAdd_Click()
    Dim lRow As Long
    Dim lPart As Long
    Dim WS As Worksheet
    Set WS = Worksheets("DetailOverShort")

    'find first empty row in database
    lRow = WS.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

    ldate = Me.txtDate.Value

    'check for a date
    If Trim(Me.txtDate.Value) = "" Then
    Me.txtDate.SetFocus
    MsgBox "Please enter a Date"

    ElseIf Trim(Me.txtDate.Value) <> "mm/dd/yyyy" Then
    Me.txtDate.SetFocus
    MsgBox "Please enter date in correct format"
    Else
    Me.txtDate.Value = ldate


    End If

    Exit Sub
    'copy the data to the database
    'use protect and unprotect lines,
    ' with your password
    ' if worksheet is protected
    With WS
    .Unprotect Password:="password"
    If MsgBox("Is all data correct?", vbYesNo) = vbNo Then

    .Cells(lRow, 2).Value = Me.txtDate.Value
    .Cells(lRow, 3).Value = Me.cboDirectIndirect.Value
    .Cells(lRow, 4).Value = Me.txtPERNR.Value
    .Cells(lRow, 5).Value = Me.txtName.Value
    .Cells(lRow, 6).Value = Me.cboLocation.Value
    .Cells(lRow, 10).Value = Me.txtAmt.Value
    .Cells(lRow, 12).Value = Me.txtCorrection.Value
    .Cells(lRow, 14).Value = Me.txtComments.Value
    .Cells(lRow, 15).Value = Me.txtKioskNo.Value
    .Cells(lRow, 16).Value = Me.txtKioskITDate.Value
    .Cells(lRow, 17).Value = Me.txtKioskClosedDate.Value
    .Cells(lRow, 18).Value = Me.txtKioskResolution.Value

    ' .Protect Password:="password"
    End If

    If cboDirectIndirect.Value = "Indirect" Then
    txtAmt.Enabled = False
    Else
    txtAmt.Enabled = True
    End If


    Exit Sub
    End With

    'clear the data
    Me.txtDate.Value = Format(Date, "Medium Date")
    Me.cboDirectIndirect.Value = ""
    Me.txtPERNR.Value = ""
    Me.txtName.Value = ""
    Me.cboLocation.Value = ""
    Me.txtAmt.Value = ""
    Me.txtCorrection.Value = ""
    Me.txtComments.Value = ""
    Me.txtKioskNo.Value = ""
    Me.txtKioskITDate.Value = ""
    Me.txtKioskClosedDate.Value = ""
    Me.txtKioskResolution.Value = ""


    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Date and data validation not working

    ISDATE should help if ISDATE(X)
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    Try using IsDate to check if a valid date has been entered.
    Please Login or Register  to view this content.
    What's the problem here, the code looks fine to me?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by nathansav View Post
    ISDATE should help if ISDATE(X)
    Thanks nathansav,
    Should I remove the ElseIf before? I added IsDate within my line of code, and I received an error:

    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Date and data validation not working

    no that should be good, your logic was right, you were just trying to check the format, which could have been 99/99/9999. You could have done this way using Regular Expressions, but anyway...

    Dates are numbers of days since 1901 i believe, they are just formatted nicely for you, so ISDATE will check that for you.

  6. #6
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by nathansav View Post
    no that should be good, your logic was right, you were just trying to check the format, which could have been 99/99/9999. You could have done this way using Regular Expressions, but anyway...

    Dates are numbers of days since 1901 i believe, they are just formatted nicely for you, so ISDATE will check that for you.
    When I attempt to submit the data via that add discrepancy button within my userform, I receive the following error. Run-time error '13': Type mismatch, and when I click on Debug, the ElseIf line is highlighted.

  7. #7
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    What's the problem here, the code looks fine to me?
    Please Login or Register  to view this content.
    [/QUOTE]

    Hi Norie,
    The userform is still letting me place an amount within the txt.Amt.Value box after selecting Indirect. I'm trying to get it so when a user chooses Indirect and then also attempts to place an amount within the txt.Amt.Value text box they are given an error message. I can see I need to add a msgbox within the code to let them know they have entered an amount after choosing Indirect, but right now nothing is happening. Of course, I'm trying to get past the date validation issue nathansav is currently helping me with.

    I commented out the date validation portion and attempted to run it to see if the direct/indirect validation was working, nothing is happening after I run it. I get no error, msgbox, or data submittion.
    [code]
    Private Sub cmdAdd_Click()
    Dim lRow As Long
    Dim lPart As Long
    Dim WS As Worksheet
    Set WS = Worksheets("DetailOverShort")

    'find first empty row in database
    lRow = WS.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

    ldate = Me.txtDate.Value

    'check for a date
    If Trim(Me.txtDate.Value) = "" Then
    Me.txtDate.SetFocus
    MsgBox "Please enter a Date"
    'ElseIf IsDate(Me.txtDate.Value) <> "mm/dd/yyyy" Then
    'Me.txtDate.SetFocus
    ' MsgBox "Please enter date in correct format"
    'Else
    'Me.txtDate.Value = ldate

    End If

    Exit Sub
    'copy the data to the database
    'use protect and unprotect lines,
    ' with your password
    ' if worksheet is protected
    With WS
    .Unprotect Password:="password"
    If MsgBox("Is all data correct?", vbYesNo) = vbNo Then

    .Cells(lRow, 2).Value = Me.txtDate.Value
    .Cells(lRow, 3).Value = Me.cboDirectIndirect.Value
    .Cells(lRow, 4).Value = Me.txtPERNR.Value
    .Cells(lRow, 5).Value = Me.txtName.Value
    .Cells(lRow, 6).Value = Me.cboLocation.Value
    .Cells(lRow, 10).Value = Me.txtAmt.Value
    .Cells(lRow, 12).Value = Me.txtCorrection.Value
    .Cells(lRow, 14).Value = Me.txtComments.Value
    .Cells(lRow, 15).Value = Me.txtKioskNo.Value
    .Cells(lRow, 16).Value = Me.txtKioskITDate.Value
    .Cells(lRow, 17).Value = Me.txtKioskClosedDate.Value
    .Cells(lRow, 18).Value = Me.txtKioskResolution.Value

    ' .Protect Password:="password"
    End If
    If cboDirectIndirect.Value = "Indirect" Then
    txtAmt.Enabled = False
    MsgBox ("You cannot enter a dollar amount when Discrepancy Type is set to Indirect!")
    Else
    txtAmt.Enabled = True
    End If
    Exit Sub
    End With

    'clear the data
    Me.txtDate.Value = Format(Date, "Medium Date")
    Me.cboDirectIndirect.Value = ""
    Me.txtPERNR.Value = ""
    Me.txtName.Value = ""
    Me.cboLocation.Value = ""
    Me.txtAmt.Value = ""
    Me.txtCorrection.Value = ""
    Me.txtComments.Value = ""
    Me.txtKioskNo.Value = ""
    Me.txtKioskITDate.Value = ""
    Me.txtKioskClosedDate.Value = ""
    Me.txtKioskResolution.Value = ""
    End Sub
    [/code
    Last edited by Damian37; 06-12-2014 at 11:29 AM. Reason: Changes to code

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    If txtAmt has been disable then the user can't enter a value in it, so you don't need a message if they enter something in it.

    Mind you, are you sure the code is in the right place?

    I would have thought it would have made more sense to have it in the change event of the combobox.

    Then when they select Indirect the textbox will be disabled and they won't be able to enter anything in it.

    Did you try the code I posted in post #2 for date validation?

  9. #9
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    If txtAmt has been disable then the user can't enter a value in it, so you don't need a message if they enter something in it.

    Mind you, are you sure the code is in the right place?

    I would have thought it would have made more sense to have it in the change event of the combobox.

    Then when they select Indirect the textbox will be disabled and they won't be able to enter anything in it.

    Did you try the code I posted in post #2 for date validation?
    Yes, the code you posted in post # 2 is the one I am using, but I am confused as to what you're referring to when you refer to the change event of the combobox. Is that the second line within the list of value parameters in the code? so like this?
    Please Login or Register  to view this content.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    No, it's an event of the combobox that's triggered when a selection is made.

    As it is, with the code you have you, are checking the value of the combobox after the user has entered something in the textbox and after the data has been entered on the worksheet.

  11. #11
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    No, it's an event of the combobox that's triggered when a selection is made.

    As it is, with the code you have you, are checking the value of the combobox after the user has entered something in the textbox and after the data has been entered on the worksheet.
    Ok, I think that's where my confusion lies, because I cannot see any sub coding for the combobox. The only reference within the coding for the combobox is the line you see within the coding I sent...
    Please Login or Register  to view this content.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    In design view double click the combobox.

  13. #13
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    In design view double click the combobox.
    OMG Norie!
    I feel like such an idiot. I've actually done that double-clicking for a few texboxes I'm currently using within my userform. Wow. Well, that did it! Thanks so much. Now on to getting this date validation portion to work and I will then be on to the next validation, lol. I cannot thank you enough for pointing out the obvious!

  14. #14
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by nathansav View Post
    no that should be good, your logic was right, you were just trying to check the format, which could have been 99/99/9999. You could have done this way using Regular Expressions, but anyway...

    Dates are numbers of days since 1901 i believe, they are just formatted nicely for you, so ISDATE will check that for you.
    Hi nathansav,
    I was called away on another project, but I've now been asked to come back to this one. I tried entering in the information you suggested within my code, but the userform is still allowing me to enter in the date however I want. I receive no message indicating my date is in the wrong format. I've attached an example of the spreadsheet with the command button that calls on the userform. Here is the code I'm using to try and validate my date:

    Please Login or Register  to view this content.
    I would also very much like to add a check that will not allow the user to enter in a date in the future or more than 7 days ago. Is that possible?

    Thanks!
    Damian37
    Attached Files Attached Files

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    Damian

    You shouldn't worry about the format of the date, you should worry that the user has actually entered a valid date.

    That can be done using IsDate, and if they have entered a valid date you can convert it to the desired format using Format.

  16. #16
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    Damian

    You shouldn't worry about the format of the date, you should worry that the user has actually entered a valid date.

    That can be done using IsDate, and if they have entered a valid date you can convert it to the desired format using Format.
    Thanks Norie,
    So with the code that I showed in my last post, I have the format being set to mm/dd/yyyy. Does this line convert it to my desired format once they hit Add discrepancy?
    Please Login or Register  to view this content.
    Also, when I go ahead and enter in the data, I get a compile error message when attempting to add the discrepancy. The error is " Argument not optional". Not entirely sure what that's referring to.

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    IsDate is a function that takes a (string) value and returns True if VBA regards that value as a valid date.

    To test if the value entered in a textbox is a valid date the code would look something like this.
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    IsDate is a function that takes a (string) value and returns True if VBA regards that value as a valid date.

    To test if the value entered in a textbox is a valid date the code would look something like this.
    Please Login or Register  to view this content.
    Thanks Norie,
    I entered in some test discrepancies and even though I entered in the date 5/14/14, it showed up in my data sheet with the year showing all four digits. Although, it's curious, the format I have set within my code is mm/dd/yyyy, yet the format within the Date of Occurrence column shows 5/14/2014 and another column in which I concatenate three columns including the date appears 05142014. Would you happen to know why that is?

  19. #19
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Damian37 View Post
    Thanks Norie,
    I entered in some test discrepancies and even though I entered in the date 5/14/14, it showed up in my data sheet with the year showing all four digits. Although, it's curious, the format I have set within my code is mm/dd/yyyy, yet the format within the Date of Occurrence column shows 5/14/2014 and another column in which I concatenate three columns including the date appears 05142014. Would you happen to know why that is?
    Nevermind, I figured out why that is. It's the formatting within the cells. I'm good. Thanks!

  20. #20
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    IsDate is a function that takes a (string) value and returns True if VBA regards that value as a valid date.

    To test if the value entered in a textbox is a valid date the code would look something like this.
    Please Login or Register  to view this content.
    Thanks for the help Norie. Using IsDate, and formatting it the way I want it, now it doesn't matter how they enter in the date as long as they enter in a valid date. I did want to ask you however if you knew how I could go about not allowing the user to enter in any future dates, i.e. 07/1/2020 or being able to enter in a date older than 7 days i.e. 06/23/2014? Would I add another ElseIf statement? Can I even do that? Or do I need to add another If statement all together?

    Please Login or Register  to view this content.
    Would something like that work?
    Last edited by Damian37; 07-01-2014 at 06:55 PM.

  21. #21
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    That's shouldn't be too much trouble.

    For that you could use something along these lines.
    Please Login or Register  to view this content.
    That code would need to come after the code that checks the date is valid.

  22. #22
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    That's shouldn't be too much trouble.

    For that you could use something along these lines.
    Please Login or Register  to view this content.
    That code would need to come after the code that checks the date is valid.
    Would it be possible to combine the two rather than have two if statements? Something like
    Please Login or Register  to view this content.

  23. #23
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Damian37 View Post
    Would it be possible to combine the two rather than have two if statements? Something like
    Please Login or Register  to view this content.
    Norie, I tried to run it with the Or statement embedded within the If statement, and it didn't work. So I added it using two if statements and it works like a charm now. Thanks!

  24. #24
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    Glad it worked.

    You could combine the 2 if statements but I thought it might be better to have them separate so you could be specific about the problem with the date the user has entered.

    PS If you don't want the user to enter a date beyond today's date you could always give them a wee hint by setting the textbox value to the current date in the userform's Initialize event.
    Please Login or Register  to view this content.

  25. #25
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    Glad it worked.

    You could combine the 2 if statements but I thought it might be better to have them separate so you could be specific about the problem with the date the user has entered.

    PS If you don't want the user to enter a date beyond today's date you could always give them a wee hint by setting the textbox value to the current date in the userform's Initialize event.
    Please Login or Register  to view this content.
    I agree about the two if statements. Also, the initialize form to have the date already setup is genius! I added it to the code and it automatically entered in today's date. There's only one glitch, after 2014 it is giving me 182 and I'm thinking this might have something to do with VBA trying to add a timestamp on to the date. How would I go about taking the 182 out?

  26. #26
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Date and data validation not working

    Oops, it should only be 4 y not 5 y.

  27. #27
    Forum Contributor
    Join Date
    04-02-2013
    Location
    Hollywood, Fl
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Date and data validation not working

    Quote Originally Posted by Norie View Post
    Oops, it should only be 4 y not 5 y.
    Oh duh! I should've caught that. Still at work and trying to get out of here.Taking the extra y did it. Thanks for all your help. I'm trying to also add validation to some of the boxes within the userform to force the user to enter in values if another box has values. I'll start a new thread since it's a separate issue, but I would really appreciate your help on it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding Date Data Validation to cells with List Data Validation
    By biggtyme in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-03-2013, 09:47 AM
  2. [SOLVED] Validation Data Not Working
    By cychua in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2013, 05:40 AM
  3. Date Validation Not Working
    By mdyoung12 in forum Excel General
    Replies: 2
    Last Post: 11-16-2012, 03:09 AM
  4. validation rules not working when someone copy paste data on validation cell
    By jthakrar in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-17-2010, 03:36 AM
  5. [SOLVED] Data Validation Not Working
    By Brett in forum Excel General
    Replies: 6
    Last Post: 03-19-2005, 10:06 AM

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