Consulting

Results 1 to 6 of 6

Thread: Formatting dates

  1. #1
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location

    Formatting dates

    In excel, i have dates in column B that are not separated by / /. For example, dates are just listed as:

    1312003 -- I would like to write a macro to get this to say 1/31/2003
    8312002 -- " " 8/31/2002
    10312002 -- " " 10/31/2002

    Is there any way can incorporate something in my macro that will look in column B and be able to separate this numbers into date form? Also, I would like to do this for all values in column B, which could range from B1:B100 or B1:B1380, etc..

    Thanks for the help

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub MakeDate()
    Dim rng As Range, cel As Range
    Dim Y, M, D
    Set rng = Range(Cells(1, 2), Cells(Rows.Count, 2).End(xlUp))
    For Each cel In rng
    Y = Right(cel, 4)
    D = Mid(cel, Len(cel) - 5, 2)
    M = Left(cel, Len(cel) - 6)
    cel.Offset(, 1) = DateSerial(Y, M, D)
    Next
    rng.Offset(, 1).NumberFormat = "mm/dd/yyyy"

    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location
    this works great! although it is changing the dates in column C and I need it to change the dates ONLY IN column B.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Remove the Offsets.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location
    oh i see, its taking the results and placing them in column c...

  6. #6
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location
    thanks for your help, works great!

Posting Permissions

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