Consulting

Results 1 to 19 of 19

Thread: vba script to convert datetime string to date format

  1. #1
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location

    vba script to convert datetime string to date format

    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
    Attached Files Attached Files
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I'd use a User Defined Function to put the dates into another column. The new column needs to have the proper number formats.

    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)
        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
    Assumes that the inputs are fixed position (faster that way). Could be changed

    You could also make a sub that just reformats Col A, but that would not be (easily) undo-able

    Paul

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    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.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Application.GetCustomListContents(3),

    THAT's what I couldn't remember


    Paul

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642

  6. #6
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    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

  7. #7
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    Thanks very much for your help snb


    Quote Originally Posted by snb View Post
    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
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    Attached Files Attached Files

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    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

  10. #10
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    I used the first option below but formula doesn't recognise it as a date in this COUNTIF(A2:A11,"> 01/09/2012")


    Quote Originally Posted by snb View Post
    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
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  11. #11
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    ok this is working perfect!! Thanks guys.. I appreciate for your time

    Quote Originally Posted by Paul_Hossler View Post
    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
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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

  13. #13
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    much appreciated!! Thanks Sam

    Quote Originally Posted by SamT View Post
    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
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  14. #14
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by snb View Post
    Why do you use variables ?
    Personal style

    I find it easier to follow the code months later when I'm 'wordy'

    My Opinion: Internally I don't think there's any performance penalty since VBA still has to push and pop all the same things to the call stack anyway

    Paul

  15. #15
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    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

  16. #16
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    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


    HTML Code:
    Set Old = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    Yeliz



    Quote Originally Posted by SamT View Post
    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
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  17. #17
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    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

  18. #18
    VBAX Mentor
    Joined
    May 2010
    Location
    London
    Posts
    307
    Location
    Thank you all for all your replies. I just want to learn how to edit below code if I run it for a specific worksheet?

    Quote Originally Posted by SamT View Post
    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
    Sub Learning VBA()

    Do
    Practice Most Useful VBA Examples
    Loop Until Become an Expert in VBA

    End Sub

  19. #19
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    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)

Posting Permissions

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