help with easy macro

thepartydj

Board Regular
Joined
Sep 23, 2004
Messages
247
I would like a macro that will go down one column deleting everything in each cell but the first 9 characters.

for example it will take a text cell with
10/3/2006 15:30:54
to
10/03/2006

I need this because I already have the data and I didn't format the column as date. Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi,

try this
Code:
Sub test()
Dim rng As Range
Dim arr As Variant
Dim i As Long
Dim LR As Long

LR = Cells(Rows.Count, 1).End(xlUp).Row

Set rng = Range("A1:A" & LR)
arr = rng

    For i = 1 To rng.Rows.Count
        arr(i, 1) = Left(arr(i, 1), 9)
    Next i

rng = arr

End Sub
(not sure if this will work, you never know with date-stuff)

for more info about this technique see http://puremis.net/excel/code/053.shtml

kind regards,
Erik
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
I don't think the above code will give the desired results.
If all you're wanting is to format these as dates then wouldn't this do?
(Or am I misunderstanding?)
Code:
Sub FormatToDates()
Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).NumberFormat = "mm/dd/yy"
End Sub
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
Got to thinking about this and (since Erik's seldom wrong) I'm wondering if you're not
wanting to just format as dates, but change the values - and appearance -
to actual dates (as in whole numbers only).
If that's the case then this may be of interest.
(Assumes you have header(s) in row 1)
Code:
Sub test2()Dim LR As Long
Dim aRng As Range, bRng As Range
LR = Cells(Rows.Count, 1).End(xlUp).Row
Set aRng = Range("A2:A" & LR)
On Error GoTo Quit
With Application
  .EnableEvents = False
  .ScreenUpdating = False
End With

Columns("B:B").Insert Shift:=xlToRight
Set bRng = Range("B2:B" & LR)
bRng.NumberFormat = "General"

With Range("B2")
  .Formula = "=ROUNDDOWN(A2,0)"
  .NumberFormat = "0"
  .AutoFill Destination:=Range("B2:B" & LR)
End With

With aRng
  .NumberFormat = "mm/dd/yy"
  .Value = bRng.Value
End With

Columns("B:B").Delete

Quit:
With Application
  .EnableEvents = True
  .ScreenUpdating = True
End With
End Sub

[EDIT:]
Changed a few of the cell references to keep the dates in their proper rows.
(Thanks goes to Erik for pointing that out.) (y)
 

Forum statistics

Threads
1,136,272
Messages
5,674,757
Members
419,525
Latest member
helensesc

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top