Solved

Excel VBA Question Date conversion

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

791 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