+ Reply to Thread
Results 1 to 2 of 2

Userform Date Format Issues

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2009
    Location
    London
    MS-Off Ver
    Excel 365
    Posts
    73

    Userform Date Format Issues

    Hi,

    I have created a user form for users to input data onto a spreadsheet. All works find but I wanted to put some error validation into it to pick up input errors. The issue I have is I can't get the CDATE function to understand an incorrect date. If the user inputs the date as 01/02/03 it will happily put 1st February 2003 - Perfect!! But if I put in 32/02/03 to generate an error the system happily puts the date as 3rd January 1932.

    Please see my code here.

        Dim DateOfJoining As Date
        Dim MaternityStartDate As Date
        Dim ExpectedWeekConfinement As Date
        Dim QualifyingWeek As Date
        Dim ForthWeek As Date
        Dim MonthOne As String
        
        Dim MonthTwo As Double
        Dim PayDay1 As Date
        Dim PayDay2 As Date
        
        Private Sub CalculateButton_Click()
    'Make Sheet Activate
        Sheet1.Activate
    
        
    
    'Insert Details
    Start:
        
        MonthOne = TextBoxDay.Value + "/" + TextBoxMonth.Value + "/" + TextBoxYear.Value
        DateOfJoining = CDate(MonthOne)
        On Error GoTo DoJerror
        MaternityStartDate = TextBoxmsDay.Value + "/" + TextBoxmsMonth.Value + "/" + TextBoxmsYear.Value
        On Error GoTo MSerror
        ExpectedWeekConfinement = TextBoxewcDay.Value + "/" + TextBoxewcMonth.Value + "/" + TextBoxewcYear.Value
        On Error GoTo EwCerror
        QualifyingWeek = ExpectedWeekConfinement - 104 - Weekday(ExpectedWeekConfinement, vbSunday)
        
     If (Weekday(ExpectedWeekConfinement, vbSunday) <> 1) _
        Then ForthWeek = ExpectedWeekConfinement - Weekday(ExpectedWeekConfinement, vbSunday) - 27 _
        Else ForthWeek = ExpectedWeekConfinement - 28
     
    
        Range("F6").Value = MonthOne
        Range("C2").Value = TextBoxName.Value
        Range("C3").Value = TextBoxNumber.Value
        Range("C5").Value = DateOfJoining
        Range("C6").Value = MaternityStartDate
        Range("C7").Value = ExpectedWeekConfinement
        Range("C8").Value = QualifyingWeek
        Range("C9").Value = ForthWeek
        
        Range("B15").Value = Format(PayDay1, "mmmm yyyy")
        Range("B16").Value = Format(PayDay2, "mmmm yyyy")
        
        Range("C15").Value = CCur(TextBoxMonth1)
        Range("C16").Value = CCur(TextBoxMonth2)
        
        Unload Me
    
    GoTo EndSub
    
    
    EwCerror:
         MsgBox "Please enter a valid date for " + Label12.Caption
              GoTo Start
    DoJerror:
         MsgBox "Please enter a valid date for " + Label4.Caption
              GoTo Start
    MSerror:
         MsgBox "Please enter a valid date for " + Label8.Caption
              GoTo Start
    
    
    
    
    EndSub:
    
    End Sub
    
    
    
    
    Private Sub CheckBox1_Click()
    
    Start:
       
        On Error GoTo DoJerror
        DateOfJoining = Day(TextBoxDay.Value) + Month(TextBoxMonth.Value) + Year(TextBoxYear.Value)
        On Error GoTo MSerror
        MaternityStartDate = TextBoxmsDay.Value + "/" + TextBoxmsMonth.Value + "/" + TextBoxmsYear.Value
        On Error GoTo EwCerror
        ExpectedWeekConfinement = TextBoxewcDay.Value + "/" + TextBoxewcMonth.Value + "/" + TextBoxewcYear.Value
        
        QualifyingWeek = ExpectedWeekConfinement - 104 - Weekday(ExpectedWeekConfinement, vbSunday)
        If (WorksheetFunction.EoMonth(QualifyingWeek, 0) >= QualifyingWeek) _
            And (WorksheetFunction.EoMonth(QualifyingWeek, 0) <= QualifyingWeek + 6) _
                Then PayDay1 = WorksheetFunction.EoMonth(QualifyingWeek, -1) Else PayDay1 = WorksheetFunction.EoMonth(QualifyingWeek, -2)
        PayDay2 = WorksheetFunction.EoMonth(PayDay1, 1)
        
        If CheckBox1.Value = False Then
        PaymentInst.Visible = False
        Month1.Visible = False
        Month1.Caption = Format(PayDay1, "Mmmm yyyy")
        TextBoxMonth1.Visible = False
        Month2.Visible = False
        Month2.Caption = Format(PayDay2, "Mmmm yyyy")
        TextBoxMonth2.Visible = False
        
        Else
            
        PaymentInst.Visible = True
        Month1.Visible = True
        Month1.Caption = Format(PayDay1, "Mmmm yyyy")
        TextBoxMonth1.Visible = True
        Month2.Visible = True
        Month2.Caption = Format(PayDay2, "Mmmm yyyy")
        TextBoxMonth2.Visible = True
        End If
        
    GoTo EndSub
    
    
    EwCerror:
         MsgBox "Please enter a valid date for " + Label12.Caption
         CheckBox1.Value = False
         GoTo Start
    DoJerror:
         MsgBox "Please enter a valid date for " + Label4.Caption
         CheckBox1.Value = False
         GoTo Start
    MSerror:
         MsgBox "Please enter a valid date for " + Label8.Caption
         CheckBox1.Value = False
         GoTo Start
    EndSub:
        
    End Sub
    
    Private Sub CommandButtonClose_Click()
    Unload Me
    
    End Sub
    Can anyone let me know where I am going wrong! Or see a better way to achieve this.
    I do not have DatePicker as an additional option in my Userform tool box and do not have admin rights on this machine to install it.

    Thank you in advance.

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

    Re: Userform Date Format Issues

    You can use IsDate to check for a valid date.

    For example.
        MonthOne = TextBoxDay.Value + "/" + TextBoxMonth.Value + "/" + TextBoxYear.Value
        If IsDate(MonthOne) Then
            MsgBox "Please enter a valid date for " + Label12.Caption
            Exit Sub
        Else
            DateOfJoining = CDate(MonthOne)
        End If

    By the way, you might want to consider checking the values in TextBoxDay, TextBoxMonth, TextBoxYear etc to see if they are valid.

    Might even be an idea to use spin buttons to restrict what goes in the textboxes.
    If posting code please use code tags, see here.

+ 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. Date and Calendar issues in Userform
    By passman86 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-01-2013, 07:18 AM
  2. MMM JJ Date Format Issues with different countries
    By wannabeexcel in forum Excel General
    Replies: 3
    Last Post: 02-10-2012, 05:54 AM
  3. Date format issues
    By robertrocks in forum Excel General
    Replies: 2
    Last Post: 11-05-2010, 01:32 PM
  4. Date format issues
    By sketchgal in forum Excel General
    Replies: 2
    Last Post: 07-20-2010, 07:33 AM
  5. date format issues
    By freekrill in forum Excel General
    Replies: 2
    Last Post: 02-16-2005, 07:52 PM

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