Reputation: 11
I have a column formatted in hh:mm:ss
within the cells there is also a yyyy-mm-dd
added.
They look something like this:
1970-01-01 00:01:25
I want it to look like this formatted in text:
00:01:25
If I go about using text to columns manually to remove the yyyy-mm-dd and only importing the 00:01:25
part as text, it works fine.
But if I create a macro doing the same I end up with:
:01:25 AM
My macro looks like this:
Columns("D:D").Select
Selection.TextToColumns Destination:=Range("D1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 9), Array(12, 2)), TrailingMinusNumbers:=True
Thank you all, I have now got it to work by useing the following code(I got help from another board:
Sub m()
With Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row)
.NumberFormat = "@"
.Cells = Evaluate("=INDEX(TEXT(" & .Address & ",""HH:MM:SS""),0)")
End With
End Sub
Upvotes: 1
Views: 3038
Reputation: 2233
It can be done using excel formula Text(text;format)
see picture and formula bar.
Or you can just apply custom text formatting to the original column.
Or do you need to do this in VBA?
Range("D:D").NumberFormat = "hh:mm:ss"
Upvotes: 1