+ Reply to Thread
Results 1 to 29 of 29

Adding leading zero to the date in the text files

  1. #1
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Smile Adding leading zero to the date in the text files

    Hi,

    I need to add a leading zero before month in a text file, but the spacing should same as before after adding the zero also.

    I tries using to open with excel and changed the format but after i did the test to columns the spacing of the old format is changed.

    Ex:

    BUARDC 04294008295 010321 6000 7/17/2010 0000354652 000224

    This is the sample data, in this i need to add zero before that "7". but the format should be same like the spacing between the letters and all the numbers also.

    If there is any macro coding available for this replacing the date format.

    Attached the input file for your reference.

    Can anyone help me on this how i can solve this issue.

    Thanks in advance
    Last edited by vinwin06; 07-31-2010 at 07:53 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding leading zero to the date in the text files

    Hello vinwin06,

    Check your post again. It doesn't look like the attachment came along.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Please find the attachment now!!
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    More information below ;

    For ex:
    BUARD2 04596009620 010327 14260 7/15/2010 0000354485 2799

    In the above example there is a two space between the date and the next item, so the leading zero should be adjusted in the two spaces, so after adding the leading zero to the "month" (07) there should be one space between them.

    After conversion the data should be like below ;

    BUARD2 04596009620 010327 14260 07/15/2010 0000354485 2799

    Kindly help me!!!

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Adding leading zero to the date in the text files

    Open your .txt file in Notepad and Click on Edit -> Replace.
    Type a "7/" in the Find what: box and "07/ in the Replace with: box.
    Replace all and that should do it.

  6. #6
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Hi Marvin,

    Please see my above reply the spacing between the text important.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding leading zero to the date in the text files

    Hello vinwin06,

    I have tested this macro on your attachment and it works correctly. You will need to change the file path and possibly the file name in the macro.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Hi,

    Thanks for your effort , but i am getting error message when i am try to change the path and file name the macro code looks like below
    Please Login or Register  to view this content.
    In this coding im getting error message path not found .

    Can you please tel me where and all i need to change the path.
    Last edited by Leith Ross; 07-31-2010 at 06:42 PM. Reason: Added Code Tags

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding leading zero to the date in the text files

    Hello vinwin06,

    The file path and file name are separate in the macro. You included the file name with the file path. See the bold corrections below.
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Thank you so much , its working great. I appreciate all your efforts.

  11. #11
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Hi Leith,

    I need one more suggestions from you, if there is any way to run the same macro for two different file names. but both files are available in the same path .
    Ex : the other file name is "ReconNet98.txt"

    Can you help me in this.

  12. #12
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Hi Leith,

    I want to understand these codes briefly, could please explain these codes. Because i wan to learn how these codes finish the process .

    Thanks for your help!!!

  13. #13
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,613

    Re: Adding leading zero to the date in the text files

    Replace:
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    Ben Van Johnson

  14. #14
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Adding leading zero to the date in the text files

    now there's a thing, been on this for days! never thought this could be done!
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  15. #15
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding leading zero to the date in the text files

    Hello vinwin06,

    I changed the macro to use the Open dialog. The directory is changed to where the files are located. You just select the text file you want to open and click "Open".
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Hi Leith,

    When i try use your codes given by you, its showing error of "method or data member not found".

  17. #17
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding leading zero to the date in the text files

    Hello vinwin06,

    On which line does the error occur?

  18. #18
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Im getting error on the below line ;

    "Filename = Application.GetOpenFilename(FileFilters, 2)"

  19. #19
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding leading zero to the date in the text files

    Hello vinwin06,

    The code runs fine on my computer. I am using Excel 2003. Are you using the same version of Excel?

  20. #20
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Ya now is working for me also, thank you so much for your efforts.

    Can you explain these codes briefly , because i am curious about to learn this codings.

  21. #21
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding leading zero to the date in the text files

    Hello vinwin06,

    I will need some time to write the explanations. I will try to post that later today.

  22. #22
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Thanks Leith.

  23. #23
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    HI Leith,

    I am waiting for your reply!!!

  24. #24
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Hi leith,

    when i can expect reply from you????

  25. #25
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Adding leading zero to the date in the text files

    Dear vinwin06.
    Please be patient. The people helping you do that in their free time and at no cost to you. I am sure if you paid them you'd have your answer right away but you can't expect volunteers to jump for you when you say so.
    Be kind to these helpful people please.

    Regards
    John

  26. #26
    Registered User
    Join Date
    07-25-2010
    Location
    chennai
    MS-Off Ver
    Excel 2016
    Posts
    48

    Re: Adding leading zero to the date in the text files

    Ok John, i am not forcing leith to give me the reply immediately. I am eager to know about it thats why, its just reminder not anything else

    Thanks

  27. #27
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Adding leading zero to the date in the text files

    I just didn't understand why my first answer didn't work. I suggested using Notepad and the Replace function. I now see I needed a second step. After replacing all "7/" with "07/" you should do a second replace of
    "2010 0000" with "2010 0000" That should keep you from running all that code. That would take the the 2010<sp><sp>0000 and make them all 2010<sp>0000.
    Save the text file and go on to the next one.

  28. #28
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Adding leading zero to the date in the text files

    I messed up. I didn't see there was a 7/17/2010 that created a problem. The fix is to search for:
    <sp>7/ and replace it with <sp>07/ This would then do your problem correctly and not pick up the 7/17/2010. You need to compress the two spaces between 2010 and 0000 as shown above.

  29. #29
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Adding leading zero to the date in the text files

    @MARVINP
    see this earlier thread http://www.excelforum.com/excel-misc...inal-file.html obviously op wanted a macro solution all along

+ 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