+ Reply to Thread
Results 1 to 6 of 6

Date format changes in Userform Textbox output

Hybrid View

  1. #1
    Registered User
    Join Date
    12-29-2024
    Location
    Melbourne Australia
    MS-Off Ver
    16
    Posts
    4

    Date format changes in Userform Textbox output

    Hi all,
    I have a userform which is use to add data to a worksheet. Textbox1 is for the date which I would like to be dd/mm/yyyy. I have a Command button to show todays date:

    Private Sub CommandButton2_Click()
    TextBox1 = Date

    End Sub

    The date appears in Textbox1 as mm/dd/yyyy......but the output to the worksheet is dd/mm/yyyy

    If I manually enter the date in Textbox1 in my preferred format (dd/mmm/yyyy) the output to the worksheet is mm/dd/yyyy

    Is there a way of having Textbox1 AND the output in dd/mm/yyyy format

    This is the code I use to populate the worksheet:

    Private Sub CommandButton1_Click()
    activesheet.Select
    Dim LastRow As Object

    Set LastRow = activesheet.Range("a65536").End(xlUp)

    LastRow.Offset(1, 0).Value = TextBox1.Value
    LastRow.Offset(1, 1).Value = TextBox2.Text
    LastRow.Offset(1, 2).Value = TextBox3.Text
    LastRow.Offset(1, 3).Value = TextBox4.Text
    LastRow.Offset(1, 4).Value = ComboBox2.Value
    LastRow.Offset(1, 5).Value = ComboBox1.Text


    MsgBox " Transaction added"

    response = MsgBox(" Do you want to add another transaction?", _
    vbYesNo)

    If response = vbYes Then
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""
    ComboBox2.Text = ""
    ComboBox1.Text = ""

    TextBox1.SetFocus

    Else
    Unload Me
    End If

    End Sub

    Thanks in advance....
    Last edited by BruceWat; 01-06-2025 at 07:26 PM. Reason: spelling

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,470

    Re: Date format changes in Userform Textbox output

    Try pasting this into the UserForm code window. Untested here.

    The name references to the various controls may need to be edited to work on your end.

    Private Sub CommandButton2_Click()
        TextBox1 = Format(Date, "dd/mm/yyyy")
    End Sub
    
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        On Error Resume Next
        Dim enteredDate As Date
        enteredDate = DateValue(TextBox1.Text)
        
        If Err.Number = 0 Then
            TextBox1.Text = Format(enteredDate, "dd/mm/yyyy")
        Else
            MsgBox "Please enter a valid date in dd/mm/yyyy format.", vbExclamation
            TextBox1.SetFocus
            Cancel = True
        End If
        On Error GoTo 0
    End Sub
    
    Private Sub CommandButton1_Click()
        ActiveSheet.Select
        Dim LastRow As Object
        Dim formattedDate As String
        
        Set LastRow = ActiveSheet.Range("A65536").End(xlUp)
        
        formattedDate = Format(DateValue(TextBox1.Text), "dd/mm/yyyy")
        LastRow.Offset(1, 0).Value = formattedDate
        LastRow.Offset(1, 1).Value = TextBox2.Text
        LastRow.Offset(1, 2).Value = TextBox3.Text
        LastRow.Offset(1, 3).Value = TextBox4.Text
        LastRow.Offset(1, 4).Value = ComboBox2.Value
        LastRow.Offset(1, 5).Value = ComboBox1.Text
    
        MsgBox "Transaction added"
        
        Dim response As VbMsgBoxResult
        response = MsgBox("Do you want to add another transaction?", vbYesNo)
        
        If response = vbYes Then
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            ComboBox2.Text = ""
            ComboBox1.Text = ""
            TextBox1.SetFocus
        Else
            Unload Me
        End If
    End Sub

  3. #3
    Registered User
    Join Date
    12-29-2024
    Location
    Melbourne Australia
    MS-Off Ver
    16
    Posts
    4

    Re: Date format changes in Userform Textbox output

    Thanks for your response....much appreciated

    This works for the dd/mm/yyyy in the Textbox1....but still the output to the worksheet is mm/dd/yyyy??

    Also, the 'Today' Command button does not work?

    Cheers

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: Date format changes in Userform Textbox output

    When you code the date into the TextBox, use the Format function without the formatting code:
    Me.TextBox1.Value = Format(Date)
    When you transcribe a date from a TextBox to a worksheet cell, force the appropriate data type:
    LastRow.Offset(1, 0).Value = CDate(TextBox1.Value)
    If you follow these two rules consistently, you won't have problems. In the TextBox, the date will be displayed to you in the format you have set in the operating system (date short), and when typing into a cell, you will actually pass the date (that is, a number), not text that looks like a date and may be subject to implicit conversions at the VBA-Excel border.

    Artik

  5. #5
    Registered User
    Join Date
    12-29-2024
    Location
    Melbourne Australia
    MS-Off Ver
    16
    Posts
    4

    Re: Date format changes in Userform Textbox output

    That worked perfectly....thank you so much

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,470

    Re: Date format changes in Userform Textbox output

    Could you post a copy of your workbook without any confidential data ? That would make it easier to troubleshoot.

+ 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. [SOLVED] Date format for userform (textbox)
    By jp16 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-22-2018, 12:42 PM
  2. [SOLVED] Date Format in Userform Textbox
    By alteredbeast in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-10-2017, 03:58 AM
  3. [SOLVED] Date format in textbox userform
    By Crispy85 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2016, 06:57 AM
  4. [SOLVED] Excel 2010 - Userform - display date from textbox in a label or textbox in 'ddd' format
    By theshybutterfly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2015, 10:54 PM
  5. [SOLVED] Excel 2007, Userform Textbox Date Format and Calendar Control Userform
    By riffology in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2014, 06:18 PM
  6. Date Format for textbox in userform
    By processchip in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-08-2011, 06:42 AM
  7. Wrong Date Format in output userform
    By ABBOV in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2010, 06:58 AM

Tags for this Thread

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