Consulting

Results 1 to 4 of 4

Thread: Convert date format

  1. #1

    Question Convert date format

    Hey there

    I have to transfer dates from a report to another sheet.
    The report has the dates in dd.mm.yyyy format, and i need them in yyyy.mm.dd
    So far i managed to get them into the desired form, but there comes the problem!
    If the day number is 12 or lower the exccel treats that as the month.
    for example, if i have 13.02.2016 it works perfectly and gives me 2016.02.13
    but if i have 07.02.2017 it gives me 2017.07.02

    Worksheets("Aktual").Cells(var, 5).Value = Format(Cells(iRow, 2), "yyyy/mm/dd")

    this is the line im usig to get what i need.
    any suggestions?

    Thank you !

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Maybe you should brute force it to avoid Regional Settings issues


    Option Explicit
     
    Sub test()
        Dim s As String, d As Long, m As Long, y As Long
        
        s = "13.02.2016"
        
        d = Left(s, 2)
        m = Mid(s, 4, 2)
        y = Right(s, 4)
        
        Range("A1").Value = Format(DateSerial(y, m, d), "yyyy.mm.dd")
    
        s = "07.02.2017"
        
        d = Left(s, 2)
        m = Mid(s, 4, 2)
        y = Right(s, 4)
        
        Range("A2").Value = Format(DateSerial(y, m, d), "yyyy.mm.dd")
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Worksheets("Aktual").Cells(var, 5) = Cdate(Format(Cells(iRow, 2), "yyyy/mm/dd"))

  4. #4
    Thank you
    Seems like brute force works once again
    With a litle adjustment it does what it has to.
    Thanks

Posting Permissions

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