Hi Everyone ,
I am looking for a script to convert date time string to date format. Can anyone help on this?
Please see column A in attached spreadsheet.
From:
2011-Sep-15 12:00:00
To:
15/09/2011
Cheers
Yeliz
Hi Everyone ,
I am looking for a script to convert date time string to date format. Can anyone help on this?
Please see column A in attached spreadsheet.
From:
2011-Sep-15 12:00:00
To:
15/09/2011
Cheers
Yeliz
Sub Learning VBA()
Do
Practice Most Useful VBA Examples
Loop Until Become an Expert in VBA
End Sub
I'd use a User Defined Function to put the dates into another column. The new column needs to have the proper number formats.
Assumes that the inputs are fixed position (faster that way). Could be changedOption Explicit Function Str2Date(s As String) As Variant Dim y As Long, m As Long, d As Long On Error GoTo NiceExit '12345678901234567890 '2011-Sep-15 12:00:00 y = Mid(s, 1, 4) Select Case LCase(Mid(s, 6, 3)) Case "jan": m = 1 Case "feb": m = 2 Case "mar": m = 3 Case "apr": m = 4 Case "may": m = 5 Case "jun": m = 6 Case "jul": m = 7 Case "aug": m = 8 Case "sep": m = 9 Case "oct": m = 10 Case "nov": m = 11 Case "dec": m = 12 End Select d = Mid(s, 10, 2) Str2Date = DateSerial(y, m, d) Exit Function NiceExit: Str2Date = CVErr(xlErrNA) End Function
You could also make a sub that just reformats Col A, but that would not be (easily) undo-able
Paul
or
=TEXT(LEFT(A11,5)&MATCH(MID(A11,6,3),{"jan","feb","mar","apr","may","jun"," jul","aug","sep","oct","nov","dec"},0)&MID(A11,9,3);"dd/mm/yyyy")
or
Function F_date_snb(c00) F_date_snb = Int(CDate(Replace(c00, Mid(c00, 6, 3), Format(Application.Match(Mid(c00, 6, 3), Application.GetCustomListContents(3), 0))))) End Function
Last edited by snb; 08-12-2013 at 06:06 AM.
Application.GetCustomListContents(3),
THAT's what I couldn't remember
Paul
I also had to look it up :
http://www.snb-vba.eu/VBA_Fill_combobox_listbox_en.html
Thanks very much Paul. Is this for to make a sub ?
Cheers
Yeliz
Sub Learning VBA()
Do
Practice Most Useful VBA Examples
Loop Until Become an Expert in VBA
End Sub
Updated to include SNB's better approach, but this is a user defined function still. Included in your example
Option Explicit Function Str2Date(s As String) As Variant Dim y As Long, m As Long, d As Long On Error GoTo NiceExit '12345678901234567890 '2011-Sep-15 12:00:00 y = Mid(s, 1, 4) With Application m = .WorksheetFunction.Match(Mid(s, 6, 3), .GetCustomListContents(3), 0) End With d = Mid(s, 10, 2) Str2Date = DateSerial(y, m, d) Exit Function NiceExit: Str2Date = CVErr(xlErrNA) End Function
Paul
Why do you use variables ?
Function F_str2date_snb(c00) F_str2date_snb = CDate(Mid(c00, 10, 2) & "-" & Application.Match(Mid(c00, 6, 3), Application.GetCustomListContents(3), 0) & "-" & Mid(c00, 1, 4)) End Function
Sub ConvertDates() Dim Old As Range Dim Cel As Range Set Old = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row) 'Range formating required to fix some goofy results. YMMV 'Try it with nd without the next 4 lines With Old .NumberFormat = "dd/mm/yyyy" .HorizontalAlignment = xlLeft End With '2011-Sep-15 12:00:00 to 15/09/2011 For Each Cel In Old Cel = Format(Mid(Cel, 6, 3) _ & "/" & Mid(Cel, 10, 2) _ & "/" & Left(Cel, 4), "dd/mm/yyyy") Next Cel End Sub
I expect the student to do their homework and find all the errrors I leeve in.
Please take the time to read the Forum FAQ
Hi Guys ,
I want to mark this thread as solved but can't find it in Thread tools as navigation changed.Any ideas??
Sub Learning VBA()
Do
Practice Most Useful VBA Examples
Loop Until Become an Expert in VBA
End Sub
Hi Sam ,
It takes two sec when I run the code in attached spreadsheet however takes ages when I run it in original workbook with multiple worksheets.
I think I need to edit below line to specify worksheet called Data. I tried by adding Worksheets("Data") just before the range but didn't work
YelizHTML Code:Set Old = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
Sub Learning VBA()
Do
Practice Most Useful VBA Examples
Loop Until Become an Expert in VBA
End Sub
sub M_snb() for each sh in sheets sh.activate [A2:A2000]=iif(A2:A2000="","",TEXT(LEFT(A2:A2000,5)&MATCH(MID(A2:A2000,6,3),{"jan","feb","mar","apr","may","jun"," jul","aug","sep","oct","nov","dec"},0)&MID(A@:A2000,9,3),"dd/mm/yyyy"))] next End sub
try adding ws name before range.Set Old = Worksheets("WSNameHere").Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
PLS DO NOT PM; OPEN A THREAD INSTEAD!!!
1) Posting Code
[CODE]PasteYourCodeHere[/CODE]
(or paste your code, select it, click # button)
2) Uploading File(s)
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.
3) Testing the Codes
always back up your files before testing the codes.
4) Marking the Thread as Solved
from Thread Tools (on the top right corner, above the first message)