Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA Question Date conversion

Posted on 2011-03-13
4
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
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 in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

688 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