Getting date from Userform (formatting issue)

Sweedler

Board Regular
Joined
Nov 13, 2020
Messages
132
Office Version
  1. 365
Platform
  1. Windows
Hello

I have a Userform that I am using to enter data into cells in a row. One of those cells is to include a date. I want the date to be formatted "YY-MM-DD" (January 5, 2020 = 20-01-05). When I have the value from the userform entered into the cell it does not act as the date that it visually has.

For example. I have entered: 21-06-06 (in cell A2) (Todays date = 23-06-19 (in cell H1)) ... =A2>P1 returns the value TRUE. Which it should clearly not.

I need help getting the formatting right, and perhaps putting in a check to make sure that there is a date entered as well.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi,

if you format all date fields to YY-MM-DD it should work.
1687179852047.png
 
Upvote 0
I have tried that, but for some reason it is not carrying over.
 
Upvote 0
Hi,

if you format all date fields to YY-MM-DD it should work.
View attachment 93878
I tried that. But it was not working. Keeps coming out as a strange format. I have tried to remake my user form to enter the date in three separate boxes (year, month and day). I was thinking I could use "worksheetfunction.DATE" .. but date does not seem to be a formula that works there. I am out of ideas right now.
 
Upvote 0
Hi,
what comes out of your UserForms TextBox is Text - you should coerce the string in to its correct datatype using one of the Type Conversion Functions like CDate

example

Rich (BB code):
With Range("H1")
        .Value = CDate(Me.TextBox1.Value)
        .NumberFormat = "YY-MM-DD"
     End With

Note: you will need to check for invalid date entry otherwise code line will error.

Dave
 
Upvote 0
Solution
Hi,
what comes out of your UserForms TextBox is Text - you should coerce the string in to its correct datatype using one of the Type Conversion Functions like CDate

example

Rich (BB code):
With Range("H1")
        .Value = CDate(Me.TextBox1.Value)
        .NumberFormat = "YY-MM-DD"
     End With

Note: you will need to check for invalid date entry otherwise code line will error.

Dave
That one worked really well. Thank you so much
 
Upvote 0
Would something simila
That one worked really well. Thank you so much
Hi,
what comes out of your UserForms TextBox is Text - you should coerce the string in to its correct datatype using one of the Type Conversion Functions like CDate

example

Rich (BB code):
With Range("H1")
        .Value = CDate(Me.TextBox1.Value)
        .NumberFormat = "YY-MM-DD"
     End With

Note: you will need to check for invalid date entry otherwise code line will error.

Dave
Would something similar be possible for the numbers coming out of the userform .. so that I do not get the "number stored as Text" error all the time?
 
Upvote 0
Would something similar be possible for the numbers coming out of the userform .. so that I do not get the "number stored as Text" error all the time?
Yes, just use the required type conversion function

example

Rich (BB code):
Private Sub CommandButton2_Click()
     With Range("H1")
        .Value = CDbl(Me.TextBox1.Value)
        .NumberFormat = "YY-MM-DD"
     End With
End Sub

Function has same issue as CDate in that you need to check TextBox contains a valid numeric value otherwise it will error - or you could use VAL function which is more forgiving.

You can read more on Type Conversion Functions in Helpfile : Type conversion functions (VBA)

Dave
 
Upvote 0
@Sweedler
Are you sure @dmt32 code worked for you?
When in the textbox you enter 20-01-05 you mean it's January 5, 2020, right?
AFAIK, CDate function will read the last 2 digit as year not day.
So, if you enter "20-01-05" in the textbox then it will be read as "20-01-2005" instead of "2020-01-05"
I suggest you to use DateSerial function to make sure which part is year, month or day.
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,653
Members
452,992
Latest member
TokugawaIesuma

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top