Dates Switching from Form to Cell

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
I have a form with 2 date fields which have some formatting options in the VBA code.

When I put in a date like 10/08/2018 in the form... when it puts the date in to the corresponding cell on submission, it's switching the date to 08/10/2018.

I've formatted the cells to dd/mm/yyyy and the code in the VBA is:

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


Private Sub CommandButton2_Click()
    With SessionsBox
        .Value = dDate2
        .NumberFormat = "dd/mm/yyyy"
    End With
End Sub




Private Sub SessionsBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.SessionsBox
        If Len(.Text) > 0 Then
            If Not .Text Like "##/##/####" Then
                MsgBox "Invalid Session date format" & Chr(10) & "Please re-enter as dd/mm/yyyy", vbCritical, "Invalid Format"
                Cancel = True
            Else
                dDate2 = DateValue(.Text)
            End If
        End If
    End With
End Sub
Any ideas on how to stop the numbers from switching please?
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
7,776
Office Version
365
Platform
Windows
Try using CDate

Edit to be more clear, eg:

Code:
Range("A1").Value = CDate(TextBox1.Text)
 
Last edited:

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
Thanks Mr the fish :)

In my code I also have this:

Code:
Cells(emptyRow, 2).Value = DateValue(DOBTextBox.Value)
Cells(emptyRow, 3).Value = CDate(SessionsBox.Value)
When I tried CDate for DOBTextBox I was getting an error if memory serves me correctly.
 

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
I changed the code to this:

Code:
Cells(emptyRow, 2).Value = CDate(DOBTextBox.Text)
Cells(emptyRow, 3).Value = CDate(SessionsBox.Text)
and no errors but it's still switching dates... example:

Form DOB = 08/03/2015, what went into the cell= 03/08/2015
Form Session Date = 06/03/2017, what went into the cell = 03/06/2017
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
7,776
Office Version
365
Platform
Windows
Change the cell to General. What is the number?
 

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
For DOB = 42219
For Session = 42889
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
7,776
Office Version
365
Platform
Windows
Ok so the dates are indeed wrong. The locale is set to UK i presume? Format cells, date. I cant replicate your problem you see. I can if i dont convert the date but not once i convert the date. Its as if the application thinks you use MM/DD/YYYY on the machine. I could convert it manually if you want?
 

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
locale is set to UK - the office language preferences are set to English (UK)... tell me more about converting it manually... I need to get this working for my client lol
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
7,776
Office Version
365
Platform
Windows
Heres a more manual way:

Code:
arrDate = Split(TextBox1.Text, "/")
If UBound(arrDate) = 2 Then Range("A1").Value = DateSerial(arrDate(2), arrDate(1), arrDate(0))
 

Forum statistics

Threads
1,081,617
Messages
5,360,044
Members
400,565
Latest member
Tommy O

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top