Merovin
Merovin

Reputation: 21

Date in Textbox shows as text in Cell Range

I have a userform set up for entering data, and one of the textboxes is coded to only accept dates in the format "dd/mm/yyyy". When this is submitted and entered into the database on excel, the date is entered in the format "mm/dd/yyyy", or as text if the entry cannot work within the "mm/dd/yyyy" format.

I am currently only using a simple bit of code to enter this information:

Sheet4.Range("M5") = TextBox2
Sheet4.Range("M5").Select
ActiveCell.FormulaR1C1 = Sheet4.Range("M5").Value
Sheet4.Range("M3").Select

I'm not sure what I'm missing to swap the date format round when it's entered to excel. It updates when I refresh the cell by double clicking into it and hitting enter, which what I tried to do with lines 2 - 4, but I can't seem to get it to work and display properly.

Any help would be greatly appreciated. Thank you!

Upvotes: 1

Views: 1639

Answers (1)

Subodh Tiwari sktneer
Subodh Tiwari sktneer

Reputation: 9976

The issue is whatever you input in a TextBox is a string so when you input a date in TextBox, it is considered as a text string not a real date.

To deal with this, convert the date string in TextBox to the real date and then write it back to the cell.

Please try this...

Sheet4.Range("M5").Value = CDate(TextBox2)

Upvotes: 1

Related Questions