Solved

Excel VBA Question Date conversion

Posted on 2011-03-13
4
279 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
Comment Utility
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
Comment Utility
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
Comment Utility
Ignore me I didn't refresh and Graham's is good
0
 

Author Closing Comment

by:Zack
Comment Utility
Thanks man.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now