Consulting

Results 1 to 7 of 7

Thread: Userform submitting a date in format "mmm-yy", but a small problem arises.

  1. #1

    Userform submitting a date in format "mmm-yy", but a small problem arises.

    Hi,
    I have a small problem, where no doubt the solution is smacking me straight in the face.

    If you can imagine a userform, with a textbox, and a submit button.

    The textbox needs to simply submit a date.

    The user inputs the date as: Mar-14
    Upon hitting submit, the date gets inserted into A1 as Mar-14 (which is correct - March 2014)... But the DAY is incorrect.
    Ideally, I want it to be 01/03/2014 when I select the cell.
    But instead it is shown as 14/03/2014 (I presume because of the year)
    But how do I make it show 01/03/2014.

    It's not causing any issues right now... But I want to fix it sooner rather than later.

    How the userform submits the date.



    How I WANT the userform to submit the date.



    Like I said, the solution is smacking me right in the face.

    The code that I'm using the format the date is:

    Range("A" & LastRow).NumberFormat = "mmm-yy"
    Ignore the columns and rows in the pictures... I'm using "A" to make things easier.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Textbox values are always strings. Did you use CDate() to convert the string to a date number before placing that value into the cell(s)?

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    kenneth,

    both CDate and DateValue functions return 14.3.2014 for "Mar-14".
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    perhaps, stg like this.

    Private Sub CommandButton1_Click()
    
    
        Range("A" & Rows.Count).End(xlUp)(2).Value = _
            DateSerial( _
                Right(TextBox1.Value, 2), _
                Application.Match(Left(TextBox1.Value, 3), Application.GetCustomListContents(3), 0), _
                1)
        
                'Right(TextBox1.Value, 2) = year
                'Application.Match(Left(TextBox1.Value, 3), Application.GetCustomListContents(3), 0) = month
                '1 = day
    
    
    End Sub
    ps: Built-in "custom list 3" is an array of abbreviated 12 months' names.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    This worked for me.

    Private Sub CommandButton1_Click()
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Cells(lr, 1) = DateSerial(Year(TextBox1.Value), Month(TextBox1.Value), 1)
    End Sub
    Last edited by david000; 03-25-2014 at 05:04 PM. Reason: double checked
    "To a man with a hammer everything looks like a nail." - Mark Twain

  6. #6
    Sorry Kenneth, I thought I had replied to your comment yesterday. I obviously didn't hit the submit button??
    My response was, no, I didn't use CDate and that I haven't actually heard of that term before. So I'll have to look it up sometime, always good to learn something new.

    Thank you everyone for your help.
    I ended up going for

    DateSerial(Year(TextBox1.Value), Month(TextBox1.Value), 1)
    Thanks David000

    As this was the easiest to implement within my current code.
    Problem solved.
    Thanks again.

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome. the simpler, the better. actually my first attempt was stg like that. but i must have made a mistake, so could not produce the desired output. then i sailed away in vba ocean. .
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •