VBA Date Format Switching

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
I have the following code:

Code:
Private Sub CommandButton1_Click()    With DOBTextBox
        .Value = dDate
        .NumberFormat = "dd/mm/yyyy"
    End With
End Sub




Private Sub DOBTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.DOBTextBox
        If Len(.Text) > 0 Then
            If Not .Text Like "##/##/####" Then
                MsgBox "Invalid DOB date format" & Chr(10) & "Please re-enter as dd/mm/yyyy", vbCritical, "Invalid Format"
                Cancel = True
            Else
                dDate = DateValue(.Text)
            End If
        End If
    End With

End Sub

When the Form runs and I put a date in, it works fine - however when it comes to this part:

Code:
Sheet1.ActivateCells(emptyRow, 1).Value = NameTextBox.Value
Cells(emptyRow, 2).Value = DOBTextBox.Value
Cells(emptyRow, 3).Value = SessionsBox.Value
Cells(emptyRow, 4).Value = GroupListBox.Value

For some of the values for DOBTextBox and SessionsBox the date flips to the US Format. Example:

DOB inputted on form = 06/03/2008
DOB copied to Excel cell = 03/06/2008

I can't see where this needs to be fixed to ensure the correct date format is copied into the cells - this is important as it has to be the correct day and month for DOB and Session.

Thanks in advance,
Lee
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Lee

When putting 'text' dates from a textbox on a userform into a cell use DateValue or CDate to convert the values to 'real' dates.
Code:
Sheet1.Activate
Cells(emptyRow, 1).Value = NameTextBox.Value
Cells(emptyRow, 2).Value = DateValue(DOBTextBox.Value)
Cells(emptyRow, 3).Value = CDate(SessionsBox.Value)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top