Converting cell value from general to date

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I have a software providing the dates in general format.
For instance, in a cell I can read the following value:

01-10-2021 10:11

meaning 1st October 2021 h 10:11 am.

I need to convert this general format in date format.

How can I figure it out?

Thank's in advance.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Sounds like it's a text date that needs coercing, if it was a valid date then it would be showing as a custom format rather than general. Try copying an empty cell, then PasteSpecial to the date cell, choose Values and Add from the options before clicking OK.
 
Upvote 0
Hello,​
what about just replacing dash with slash ?​
 
Upvote 0
The result is:

44470,42639

And then?

ps. I need a vba solution.
 
Upvote 0
The result is:

44470,42639
That is actually correct. That is just your date in "General" format. If you change it to your desired Date/Time format, you will see it the way you are used to.

The reason for this is that Excel actually stores dates and times as numbers, specifically the number of days since 1/0/1900. So time is just the fraction (decimal) of one day.
If you enter a date in any cell, and change the format of the cell to "General", you will see it as Excel does, which looks like the large number you posted.
 
Upvote 0
Yes, I tried to convert with the following operation (cell is the value in the cell):

VBA Code:
cell = Format(cell, "dd/mm/yyyy hh:mm")

But something gets wrong.

For instance:

01-10-2021 10:11

is converted in
10th January 2021, not in 1st October 2021
 
Upvote 0
Yes, I tried to convert with the following operation (cell is the value in the cell):

VBA Code:
cell = Format(cell, "dd/mm/yyyy hh:mm")

But something gets wrong.

For instance:

01-10-2021 10:11

is converted in
10th January 2021, not in 1st October 2021
That is because VBA does NOT use your Regional Date Settings, and always uses the American date format.
You may need to explicitly define each component of the date, using the DateSerial function in VBA (VBA Dates & Times - Functions) and then adding the time to it.
 
Upvote 0
You need to format the cell, not the value that is in the cell. The way that you are trying to do it will most likely still leave you with a text string, not a valid date.
VBA Code:
cell.NumberFormat = "dd/mm/yyyy hh:mm"
 
Upvote 0
That is because VBA does NOT use your Regional Date Settings, and always uses the American date format.
You may need to explicitly define each component of the date, using the DateSerial function in VBA (VBA Dates & Times - Functions) and then adding the time to it.
I'm tinkering about, but it's a tough nut to crack...
 
Upvote 0
The tough part for me is that since I am using the American version myself, it is hard for me to replicate your exact scenario (so what works for me may not work for you).

My thought was since your value is a string, you can use LEFT and MID functions along with DateSerial to build the date you need, something like:
Excel Formula:
dte = DateSerial(Mid(cell,7,4),Mid(cell,4,2),Left(cell,2))
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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