Dates problem - text to numbers

shendik

New Member
Joined
Aug 22, 2011
Messages
15
So I needed to extract the "062711" part of the following string:
B1 = "110623 Manufacturing Detail_CMO_MASTER_062711.xlsm"
So I used the following combination of text functions in a different cell:
"(MID(Sheet1!B1,LEN(Sheet1!B1)-10,6)"
and this gave me the "062711" that I was looking for.

I understand that while this looks like a number, but excel considers it a text. So I used the "value" function to convert this text to a number:
"VALUE((MID(Sheet1!B1,LEN(Sheet1!B1)-10,6))"

The problem that I am having is that when I format this number "062711" as a date, it is showing as September 10, 2071 instead of July 27, 2011. This is ofcourse not what I want.

Is there an elegant solution by which I can essentially tell excel to consider the first 2 digits as month, next 2 as date, and the following two as year? Since I have large amount of texts such as the one shown above, if there is a code etc. that I can pop into a macro, it would be very helpful.

I realize that is quite a common problem with Excel but am still having trouble with it. Please share your ideas on how to deal with this. Thanks!
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Excel stores Dates as number, specifically the number of days since 1/1/1900. So if you convert a number to a date, that is the logic it uses (if you change any date to General format, you will see how it is stored).

Try using the Date(year,month,day) function instead, i.e.
Code:
=DATE("20" & MID(Sheet1!B$1,LEN(Sheet1!B$1)-6,2),MID(Sheet1!B$1,LEN(Sheet1!B$1)-10,2),MID(Sheet1!B$1,LEN(Sheet1!B$1)-8,2))
It could also be made into a User Defined Function also, which can be used directly on the spreadsheet or in a macro.
 
Upvote 0
Here is what that UDF would look like:

Code:
Function MyGetDate(myInput As String) As Date
 
    Dim MonthPiece As Integer
    Dim DayPiece As Integer
    Dim YearPiece As Integer
 
    MonthPiece = Mid(myInput, Len(myInput) - 10, 2)
    DayPiece = Mid(myInput, Len(myInput) - 8, 2)
    YearPiece = "20" & Mid(myInput, Len(myInput) - 6, 2)
 
    MyGetDate = DateSerial(YearPiece, MonthPiece, DayPiece)
 
End Function
(of course, you may want to add error handling in case an entry doesn't meet the criteria, or it is run on the wrong cell).

Then you would just use it like any other function. From a spreadsheet, it would look like:
=MyGetDate(B1)
 
Upvote 0
Another way:

=--TEXT(MID(Sheet1!B1,LEN(Sheet1!B1)-10,6), "00-00-00")
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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