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.
 
@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.
It did work well for me. I tested to make sure that it had a date value and it works.
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
It did work well for me. I tested to make sure that it had a date value and it works.
Ok, then can you show us the code?
I'm curious because it doesn't work for me on the example that I gave you.
 
Upvote 0
Ok, then can you show us the code?
I'm curious because it doesn't work for me on the example that I gave you.
Here is the entire code for the portion that includes that bit of code.

Private Sub hcplaggtill_Click()

Me.Hide

Range("B2") = Application.WorksheetFunction.VLookup(cboCourseTee, Sheets("ADMIN").Range("A1:F1000"), 2, False)
Range("C2") = Application.WorksheetFunction.VLookup(cboCourseTee, Sheets("ADMIN").Range("A1:F1000"), 3, False)
Range("D2") = Application.WorksheetFunction.VLookup(cboCourseTee, Sheets("ADMIN").Range("A1:F1000"), 4, False)

Range("E2").Value = Me.hcpexakthcp.Value
Range("G2").Value = Me.hcpslag.Value
Range("H2").Value = Me.hcppoang.Value

Range("K2") = Application.WorksheetFunction.VLookup(cboCourseTee, Sheets("ADMIN").Range("A1:F1000"), 5, False)
Range("L2") = Application.WorksheetFunction.VLookup(cboCourseTee, Sheets("ADMIN").Range("A1:F1000"), 6, False)

With Range("A2")
.Value = CDate(Me.hcpar.Value)
.NumberFormat = "YY-MM-DD"
End With

Me.cboCourseTee = ""
Me.hcpar = ""
Me.hcpexakthcp = ""
Me.hcppoang = ""
Me.hcpslag = ""
Me.OptionButton1 = False
Me.OptionButton2 = False


End Sub


Not sure if the fact that I am in Sweden helps the programming/ code as year-month-day is our default
 
Upvote 0
Not sure if the fact that I am in Sweden helps the programming/ code as year-month-day is our default
Ah, I think that's why. you're excel date setting is year-month-day, I thought it was month-day-year or day-month-year.
Thank you for showing us the code & the explanation. (y)
 
Upvote 0

Forum statistics

Threads
1,224,739
Messages
6,180,674
Members
452,993
Latest member
FDARYABEE

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