Excel VBA date issues

Tim Timmers

New Member
Joined
May 22, 2015
Messages
2
Hi,

I have a user form in which I enter a date via a textbox.
The code behind the form stores the dat in a specific cell. Later I read back the date information from this cell and notice that excel gets the date wrong. When I enter the date 6 september 2016 (actually I key in 6-9-2016) the date is displayed in the excel sheet as 6-sep-2016 but when I read back the cell to display the date in a second userform the afore mentioned date comes back as 9 june 2016.

How do I get the correct date back?

I am working in a Windows 7 environment with Excel 2007.
Region an language settings in Windows under the Formats tab: English (UK); short date dd-MMM-yyyy
Location tab: Netherlands.

Can anyone help?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
It looks as if the problem is that you are switching date formats at some point ....

Is the date stored as a date field (e.g. 42619) or is it a text field (e.g 09-06-2016). To be safest I'd make sure it is stored as a date field.
 
Upvote 0
Hi,

I modifies my code a little and it now seems to work.
In the old situation writing /reading the date to/from a cell and formatting it as a date was done in two lines of code. I have now changed it to one line of code like:

<<some lines of code<some code="" of="" lines="">>>
.TextBoxFDate = Format$(Range("D45").Value, "short date") 'read the date from cell D45 and display it in a textbox on a userform
<< more lines of code>>

Regrads, Tim</some>
 
Last edited:
Upvote 0
I had a similar problem for quite a while and there is a simple solution.

When entering the date in the textbox, that pushes it to the CELL, the problem comes from the cell which is usually set to a date format.

Change the cell format on the sheet to "text".

Now when you enter the date in the textbox, it pushes the text as is written with no alterations.

When you display this CELL in another userform or textbox, it will read it correctly if you set the date format correctly.

Prior to doing this, sometimes the date would be correct..othertimes it would be wrong. It seemed to have a mind of its own.
 
Upvote 0

Forum statistics

Threads
1,216,146
Messages
6,129,142
Members
449,488
Latest member
qh017

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