What is the solution for date format in the excel VBA form?

Azim Azrul 41 Reputation points
2021-07-15T05:28:15.83+00:00

Im still new with the excel VBA. When I key in the date in the VBA form using this format (dd/mm/yyyy), it will interpret automatically as mm/dd/yyyy in the worksheet. My computer system settings for the date format is in dd/mm/yyyy and the excel date format is dd/mm/yyyy (I tried to put it as General and custom, both didnt work). Is there any solution for this? The VBA coding might help I guess.

Thanks in advance for your help guys!

Office Visual Basic for Applications
Office Visual Basic for Applications
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Visual Basic for Applications: An implementation of Visual Basic that is built into Microsoft products.
1,496 questions
{count} votes

Accepted answer
  1. OssieMac 416 Reputation points
    2021-07-24T06:50:35.86+00:00

    I am assuming that by VBA form you mean a Userform.

    TextBoxes on Userforms are just that. They accept entries as Text. Therefore when getting the text, it needs to be converted to a date in the required format and then written to the worksheet.

    Vice versa. In getting a date from the worksheet and writing to a Userform text box then convert the date to text.

    The following example gets the entered date in a text box and converts it to a date value as a serial number.
    The split function separates the text date into day, month and year and saves the values in a zero based array (with 3 elements) as follows.
    arrSplit(0) is the Day
    arrSplit(1) is the Month
    arrSplit(2) is the Year

    DateSerial function converts the separate values to a serial date and assigns the value to a date variable.

    The date variable can then be written to the worksheet with the destination number format set as required.

    Write the date from the TextBox to the worksheet

    Private Sub cmdCopyDate_Click()
    Dim arrSplit As Variant
    Dim dte As Date
    Dim ws As Worksheet
    Dim rngDate As Range

    Set ws = Worksheets("Sheet1")
    arrSplit = Split(Me.txtDate, "/")
    dte = DateSerial(arrSplit(2), arrSplit(1), arrSplit(0))
    With ws
        Set rngDate = .Cells(2, "A")    'Alternatively .Range("A2")
    End With
    rngDate.NumberFormat = "dd/mm/yyyy"
    rngDate.Value = dte
    

    End Sub

    Get the Date from the worksheet and write it to a TextBox on the Userform

    Private Sub cmdGetDate_Click()

    Dim ws As Worksheet
    Dim rngDate As Range
    
    Set ws = Worksheets("Sheet1")
    With ws
        Set rngDate = .Cells(2, "A")    'Alternatively .Range("A2")
    End With
    
    Me.txtDate = Format(rngDate.Value, "dd/mm/yyyy")
    

    End Sub


6 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. R. Romero 0 Reputation points
    2024-07-04T06:25:19.1566667+00:00

    The issue that I have is when I add dates to a textbox in the userform

    some times it does in dd/mm/yyyy and other for a extrange it is in MM/DD/yyyy then the current system date that is for example 4 of july it came as 7 of April :(

    For i = 1 To DateRange

    Dia2Add = DateAdd("d", i, DateIni)

    MsgBox i & ": " & Dia2Add

    I_Fecha.AddItem Dia2Add

    Next i

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.