+ Reply to Thread
Results 1 to 21 of 21

macro to convert YYYYMMDD into DD/MM/YYYY

Hybrid View

  1. #1
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    macro to convert YYYYMMDD into DD/MM/YYYY

    Hi all,

    Just like I mentioned in the topic name, I need to convert dates, originally they are in the following format: YYYYMMDD, but I want to convert it to DD/MM/YYYY (notice that slashes are added and the order is completely changed).
    For example, 20080522 should be converted to 22/05/2008.
    Many thanks in advance for your help.

    Karol

  2. #2
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Hi -

    Play around with LEFT,MID and RIGHT function.

    Regards,
    event

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,042

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Use:

    Range("I18") = Evaluate("=DATE(LEFT(H18,4),MID(H18,5,2),RIGHT(H18,2))")
    Never use Merged Cells in Excel

  4. #4
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    thank you guys
    what if I have almost whole column of such dates? And I need to apply it to 2 columns actually. For column F I tried to do it like this (range starts from F11 and goes to the very bottom):
           With TheNewWB.ActiveSheet
          lastRow = .Cells(Rows.Count, "F").End(xlUp).Row
          .Range("F11:F" & lastRow).Formula = "=DATE(LEFT(F11,4),MID(F11,5,2),RIGHT(F11,2))"
       End With
    but it doesn`t work
    P.S. don`t pay attention to TheNewWB, it doesn`t matter here

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Vog,
    Zbor code is correct. You have not followed his formula.
    This should work

    Sub testev()
          With ActiveSheet
          lastRow = .Cells(Rows.Count, "F").End(xlUp).Row
          .Range("F11:F" & lastRow).Formula = Evaluate("=DATE(LEFT(F11,4),MID(F11,5,2),RIGHT(F11,2))")
       End With
    End Sub

  6. #6
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    thank you, I see that the word Èvaluate is missing in my code - what does this word exactly mean here?

  7. #7
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,042

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Quote Originally Posted by Vogelmann View Post
    thank you, I see that the word Èvaluate is missing in my code - what does this word exactly mean here?

    Evaluate Method converts a Microsoft Excel name to an object or a value

    Here's what it can do:

    1. Converts string math expressions to values.
    2. Converts 1D and 2D string arrays to their array equivalents.
    3. Capable of processing any formula a worksheet cell can process!

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    This line is also wrong

    With TheNewWB.ActiveSheet

  9. #9
    Forum Contributor
    Join Date
    09-27-2012
    Location
    London, England
    MS-Off Ver
    2003, 2010
    Posts
    344

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Try the below code. This will work in all dates in Column F (starting F11 and going all the way down). The results will be in DD/MM/YYYY format in adjacant cell (Column E)

    Sub Dates()
    Dim R As Range
    Range("F11", Range("F" & Rows.Count).End(xlUp)).Select
    For Each R In Selection
    R.Offset(0, 1) = Left(R, 4) & "/" & Mid(R, 5, 2) & "/" & Right(R, 2)
    Next R
    End Sub
    Last edited by kbkumar; 10-29-2012 at 10:30 AM.
    If solved kindly remember to mark Thread as solved.
    Click the small star icon at the bottom left of my post if this was useful.

  10. #10
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    by the way this code:
    Sub testev()
          With ActiveSheet
          lastRow = .Cells(Rows.Count, "F").End(xlUp).Row
          .Range("F11:F" & lastRow).Formula = Evaluate("=DATE(LEFT(F11,4),MID(F11,5,2),RIGHT(F11,2))")
       End With
    End Sub
    does not work properly, it gives the same value in all cells in the range.

  11. #11
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    AB33 what is wrong in that line?

    kbkumar thank you for your code, this is exactly what I need, however, when running the macro, the error `object required`is appearing and the following line is being highlighted:
    R.Offset(0, 1) = Left(R, 4) & "/" & Mid(R, 5, 2) & "/" & Right(R, 2)

  12. #12
    Forum Contributor
    Join Date
    09-27-2012
    Location
    London, England
    MS-Off Ver
    2003, 2010
    Posts
    344

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Where are you pasting the code? In the respective sheet or any module?

    This code is going to work in the sheet containing the data only.

  13. #13
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    The whole macro jumps between different workbooks and sheets, but the sheet with the data that we need is already activated before running this code.

  14. #14
    Forum Contributor
    Join Date
    09-27-2012
    Location
    London, England
    MS-Off Ver
    2003, 2010
    Posts
    344

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Are you able to upload a sample file (post removing any sensitive data)? As the code runs absolutely fine for me when i try.

    Thanks.

  15. #15
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    kbkumar.xlsm

    here you are kbkumar

  16. #16
    Forum Contributor
    Join Date
    09-27-2012
    Location
    London, England
    MS-Off Ver
    2003, 2010
    Posts
    344

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    I pasted the below code into "Sheet 1" and it worked just fine (slightly modified to factor in 2 columns and pasting the results in Columns H and I respectively:

    Sub Dates()
    Dim R As Range
    Range("F11", Range("F" & Rows.Count).End(xlUp)).Select
    For Each R In Selection
    R.Offset(0, 2) = Left(R, 4) & "/" & Mid(R, 5, 2) & "/" & Right(R, 2)
    Next R
    Range("G11", Range("G" & Rows.Count).End(xlUp)).Select
    For Each R In Selection
    R.Offset(0, 2) = Left(R, 4) & "/" & Mid(R, 5, 2) & "/" & Right(R, 2)
    Next R
    End Sub
    I also tried my pasting it into a Module and when i run the macro while being in Sheet 1 it again works fine.
    Last edited by kbkumar; 10-29-2012 at 12:15 PM.

  17. #17
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    it works!!
    I changed the letter from R to z, and I did not know that R.Offset must be changed to z.offset, that`s all

    how to make the data stay in the same column? i.e. if I originally have the data in columns F and G, I still want the output to be in the same columns instead of H and I.
    Note: changing z.offset(0,2) to z.offset(0,0) does not do the job
    Last edited by Vogelmann; 10-29-2012 at 11:50 AM.

  18. #18
    Forum Contributor
    Join Date
    09-27-2012
    Location
    London, England
    MS-Off Ver
    2003, 2010
    Posts
    344

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Glad it worked. Initial observation points that you had used R as a variable already hence it was not working (my guess). Regarding your point to have the results in the same column, please see below slightly modified code:

    Sub Dates()
    Dim Z As Range
    Range("F11", Range("F" & Rows.Count).End(xlUp)).Select
    For Each Z In Selection
    Z = Left(Z, 4) & "/" & Mid(Z, 5, 2) & "/" & Right(Z, 2)
    Next Z
    Range("G11", Range("G" & Rows.Count).End(xlUp)).Select
    For Each Z In Selection
    Z = Left(Z, 4) & "/" & Mid(Z, 5, 2) & "/" & Right(Z, 2)
    Next Z
    End Sub
    Note, that i have changed all variables from R to Z as you mentioned that Z had worked for you earlier.
    Last edited by kbkumar; 10-29-2012 at 12:14 PM.

  19. #19
    Forum Contributor
    Join Date
    09-27-2012
    Location
    London, England
    MS-Off Ver
    2003, 2010
    Posts
    344

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    Just realized that there is no need to have the loop twice, by selecting both columns in once single instance.

    Sub Dates()
    Dim Z As Range
    Range("F11", Range("G" & Rows.Count).End(xlUp)).Select
    For Each Z In Selection
    Z = Left(Z, 4) & "/" & Mid(Z, 5, 2) & "/" & Right(Z, 2)
    Next Z
    End Sub

  20. #20
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    thank you again kbkumar
    cheers

  21. #21
    Forum Contributor
    Join Date
    09-27-2012
    Location
    London, England
    MS-Off Ver
    2003, 2010
    Posts
    344

    Re: macro to convert YYYYMMDD into DD/MM/YYYY

    No worries. Please could you mark the thread as solved in case your queries have been addressed.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1