+ Reply to Thread
Results 1 to 6 of 6

Converting numbers to date format

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-31-2012
    Location
    Los Angeles, USA
    MS-Off Ver
    2003, 2010, 365
    Posts
    320

    Converting numbers to date format

    Hi,

    I have a few columns E,X and Z with the dates in the form of 20120426 and I need to convert it to 04/26/2012 format using VBA in Excel 2003.

    Any help is appreciated.

    Thanks,

    David
    Last edited by djfscouse; 10-25-2012 at 11:19 AM. Reason: Forgot to mention in Excel 2003

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Converting numbers to date format

    David,

    Give this a try:
    Sub tgr()
        
        Dim rngArea As Range
        Dim lCalc As XlCalculation
        
        With Application
            lCalc = .Calculation
            .Calculation = xlCalculationManual
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        On Error GoTo CleanExit
        
        For Each rngArea In Range("E:E,X:X,Z:Z").SpecialCells(xlCellTypeConstants).Areas
            With rngArea
                If .NumberFormat <> "mm/dd/yyyy" Then
                    .Value = Evaluate("If(IsNumber(" & .Address & "),--Text(" & .Address & ",""0000\/00\/00"")," & .Address & ")")
                    .NumberFormat = "mm/dd/yyyy"
                End If
            End With
        Next rngArea
        
    CleanExit:
        With Application
            .Calculation = lCalc
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
        If Err.Number <> 0 Then
            MsgBox Err.Description, , "Error: " & Err.Number
            Err.Clear
        End If
        
        Set rngArea = Nothing
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Contributor
    Join Date
    07-31-2012
    Location
    Los Angeles, USA
    MS-Off Ver
    2003, 2010, 365
    Posts
    320

    Re: Converting numbers to date format

    Thanks tigeravatar,

    But unfortunately it did not work, here is an example to the data in xls file.

    Thanks,

    David
    Attached Files Attached Files

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Converting numbers to date format

    djfscouse,

    From your original post:
    Quote Originally Posted by djfscouse View Post
    I have a few columns E,X and Z with the dates in

    However, your provided example file has the dates in columns E, Y, and AA. The macro is easy enough to adjust though:
    'Change this line
    For Each rngArea In Range("E:E,X:X,Z:Z").SpecialCells(xlCellTypeConstants).Areas
    
    'To be this instead
    For Each rngArea In Range("E:E,Y:Y,AA:AA").SpecialCells(xlCellTypeConstants).Areas

  5. #5
    Forum Contributor
    Join Date
    07-31-2012
    Location
    Los Angeles, USA
    MS-Off Ver
    2003, 2010, 365
    Posts
    320

    Re: Converting numbers to date format

    Thanks again tigeravatar, sorry for the original info.

  6. #6
    Forum Contributor
    Join Date
    09-23-2008
    Location
    Mexico
    Posts
    200

    Re: Converting numbers to date format


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1