Solved

Excel VBA Question Date conversion

Posted on 2011-03-13
4
313 Views
Last Modified: 2012-05-11
Heya Guys/Gals,

How would you convert the following date format 20110103 Year-Month-Day using excel VBA to the date format Day-Month-Year Hr-min-ss. I have tried to convert the date via the excel cell format cells function and it didn't work kept getting #####.

Any assistance would be much appreciated.

Thank you.
0
Comment
Question by:Zack
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 35124258
This creates a date from the string, and then formats it.

If you want to display it in an Excel cell, it would be best to put the date value into the cell and use Cell formatting to display it as you want.
Sub FormatDate()
    Dim dt As Date
    
    dt = GetDateFromString("20110103")
    MsgBox Format(dt, "ddd-MMMM-yyyy hh-mm-ss")
End Sub


Function GetDateFromString(strIndate As String) As Date
    GetDateFromString = DateSerial(Mid(strIndate, 1, 4), Mid(strIndate, 5, 2), Mid(strIndate, 7, 2))
End Function

Open in new window

0
 
LVL 10

Expert Comment

by:Makrini
ID: 35124276
Sub tester()
    datestr = Sheets("Sheet1").Range("A1").Value
    thedate = DateSerial(Left(datestr, 4), Mid(datestr, 5, 2), Right(datestr, 2))
    MsgBox Format(thedate, "dd/mm/yyyy hh:mm:ss")
    
End Sub

Open in new window


0
 
LVL 10

Expert Comment

by:Makrini
ID: 35124280
Ignore me I didn't refresh and Graham's is good
0
 

Author Closing Comment

by:Zack
ID: 35124557
Thanks man.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question