+ Reply to Thread
Results 1 to 4 of 4

Keep leading zeros?

  1. #1
    Registered User
    Join Date
    04-11-2011
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    4

    Keep leading zeros?

    Hi!
    I have been struggling with this for a few hours and have exhausted every avenue i can think of.
    I have a text file with several hundred strings of text. Each string is of varying length. I wish to extract the last four numbers from each string. The numbers are separated by spaces and vary between 0 and 9999, so i cannot do a simple RIGHT function.
    What i have tried is the following;
    I found quite a nice macro that reverses the text in a string. I use this on each line, thereby putting the numbers i want at the start of the string, but backwards. I then use text to columns, using space as the delimiter. I then use the same reverse function as above on each of the fields i require.
    THE PROBLEM!
    If any of the numbers end in 0, after i reverse the string and then delimit it the zero is lost because it is treated as a leading zero. I have tried formatting the fields both before and after the delimitation, either as text fields or by adding in zeros with a custom format and nothing works.
    Does anyone know of a way to keep leading zeros after a string is delimited? Any other suggestions as to how to solve my problem are more than welcome too!
    Thanks,
    Cj
    Last edited by cjki; 04-11-2011 at 02:38 PM. Reason: Solved.

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Keep leading zeros?

    When you apply the Text to Columns, in Step 3 of the Wizard, select the column(s) with the possible leading zeroes and then select Text from the column data format section. This will keep the format as text.. then proceed with finishing the wizard.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Contributor bentleybob's Avatar
    Join Date
    02-27-2009
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    644

    Re: Keep leading zeros?

    Why not add an "x" (or some other character) to the data before you run your macro, then remove the x from the first character in the results using SUBSTITUTE?

  4. #4
    Registered User
    Join Date
    04-11-2011
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2007
    Posts
    4

    Smile Re: Keep leading zeros?

    Thanks a million NBVC, cracked it in one! It's always something simple yet overlooked.

    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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