Solved

Excel VBA Question Date conversion

Posted on 2011-03-13
4
299 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 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