1

my userform to sign out equipment is automatically changing the date to US format (e.g. mm/dd/yyyy), even though it is being completed using UK format date (i.e. dd/mm/yyyy). Is there something I could change or add a line into the code to stop this from happening?

Private Sub CommandButton1_Click()
Dim A As Long
Sheets("Inventory").Select
Columns("A:E").Select
ActiveSheet.Unprotect
Sheets("MainPage").Select

A = Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Row + 1
    Sheet2.Cells(A, 1).Value = ComboBox1.Value
    Sheet2.Cells(A, 2).Value = Clienterva.Value
    Sheet2.Cells(A, 3).Value = Physerva.Value
    Sheet2.Cells(A, 4).Value = Dateerva.Value
    ComboBox1.Value = ""
    Patienterva.Value = ""
    Physerva.Value = ""
    Dateerva.Value = ""


Sheets("Inventory").Select
Columns("A:E").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("MainPage").Select
End Sub

Private Sub CommandButton2_Click()
UserForm1.Hide
End Sub

Private Sub Noxerva_Change()
Me.Noxerva = UCase(Me.Noxerva)
End Sub


Private Sub Clienterva_Change()
Me.Patienterva = UCase(Me.Patienterva)
End Sub

Private Sub Physerva_Change()
Me.Physerva = UCase(Me.Physerva)
End Sub

Thank you in advance

2
  • Have you tried setting the date format you want to the cell where you write it? This may be useful. Side note: in general, you want to avoid using select in your code Commented May 13 at 17:49
  • Assuming your regional settings are UK, use CDate - i.e. Sheet2.Cells(A, 4).Value = CDate(Dateerva.Value) Commented May 15 at 10:15

2 Answers 2

1

Convert String to Date

  • In your case, where e.g. today's date is the string "13/05/2025", you could use the following function to convert this string to a (VBA) date:
Function StringToDate(ByVal DateString As String) As Date
    Dim Arr() As String: Arr = Split(DateString, "/")
    StringToDate = DateSerial(Arr(2), Arr(1), Arr(0))
End Function
  • You could utilize it in the following way:
Sheet2.Cells(A, 4).Value = StringToDate(Dateerva.Value)
  • You could test the function with something like the following:
Sub Test()
    Const DateString As String = "13/05/2025"
    Debug.Print StringToDate(DateString) ' resulting US date: '5/13/2025'
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

You can use DateValue(): like:

Sheet2.Cells(A, 4).Value = DateValue(Dateerva.Value)

DateValue honours the regional settings, unless the input is wrong for that setting. i.e. if your regional date format is dd/mm/yyyy, and you provide 05/14/2025, then it tries to interpret this as 14-May-2025. It is best to check the input value before relying on it as a as a date.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.