PDA

View Full Version : Formatting dates



Klartigue
11-29-2011, 01:32 PM
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

mdmackillop
11-29-2011, 02:33 PM
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

Klartigue
11-29-2011, 02:36 PM
this works great! although it is changing the dates in column C and I need it to change the dates ONLY IN column B.

mdmackillop
11-29-2011, 02:40 PM
Remove the Offsets.

Klartigue
11-29-2011, 02:56 PM
oh i see, its taking the results and placing them in column c...

Klartigue
11-29-2011, 03:23 PM
thanks for your help, works great!