+ Reply to Thread
Results 1 to 7 of 7

Substitute characters with leading zeros in multiple scenarios

  1. #1
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Substitute characters with leading zeros in multiple scenarios

    Hi All–

    I have a spreadsheet I work with which contains a formula that needs to take an item ID letter/number combo, substitute the preceeding letter(s) with leading zeros (if the initial numerical characters are fewer than 5) to make the result return a 5-digit number.

    The original ID can be in several possible formats (beside each I will post the desired result):
    • V7684 07684
    • V366 00366
    • V88 00088
    • V60827 60827
    • VS57871 57871
    • VS67 00067


    I have the following formula which works like a charm for all except the last example:
    =IF(LEN(K2)>5,(SUBSTITUTE(SUBSTITUTE(K2,"VS","),"V",")),(REPT("0",5-LEN(K2))&(SUBSTITUTE(SUBSTITUTE(K2,"VS","0"),"V","0"))))

    If the first 2 characters are letters ("VS") and there are fewer than 5 numerical characters, the formula results in a 4-digit number, rather than 5.
    Using the above example, VS67 returns 0067 instead of 00067.

    I'm sure it's something silly that I'm overlooking but I cannot figure out what. Can anyone offer a suggestion please?

    Thanks!
    Jerry

    P.S. I just tried
    =IF(LEN(K2)>5,(SUBSTITUTE(SUBSTITUTE(K2,"VS",""),"V","")),(REPT("0",5-LEN(K2))&(SUBSTITUTE(SUBSTITUTE(K2,"VS","00"),"V","0")))) which only works when there are 3 or fewer numerical characters.
    Last edited by jerrydiaz; 03-18-2014 at 09:47 AM.

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Substitute characters with leading zeros in multiple scenarios

    Here is an Arrayed formula that works

    =TEXT(RIGHT(A1,LEN(A1)-MATCH(TRUE, ISNUMBER(MID(A1&"00000",ROW($A$1:$A$6),1)+0),0)+1),"00000")

    ...confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. You will know the array is active when you see curly braces { } appear around your formula. If you do not CTRL+SHIFT+ENTER you will get an error or a clearly incorrect answer. Press F2 on that cell and try again.
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert RobertMika's Avatar
    Join Date
    06-22-2009
    Location
    Haverhill, UK
    MS-Off Ver
    Excel 2003-13
    Posts
    1,530

    Re: Substitute characters with leading zeros in multiple scenarios

    Try:
    =IF(ISNUMBER(--MID(A2,2,1)),0&MID(SUBSTITUTE(A2," ",""),2,4),"00"&MID(SUBSTITUTE(A2," ",""),3,3))
    If you are http://www.excelforum.com/image.php?type=sigpic&userid=125481&dateline=1392355029happy with the results, please add to the contributor's
    reputation by clicking the reputation icon (star icon).




    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.
    To undo, select Thread Tools-> Mark thread as Unsolved.
    http://www.excelaris.co.uk

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

    Re: Substitute characters with leading zeros in multiple scenarios

    assuming you only have V or VS
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    gives
    V7684 07684
    V366 00366
    V88 00088
    V60827 60827
    VS57871 57871
    VS67 00067
    Last edited by martindwilson; 03-18-2014 at 10:08 AM.
    "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

  5. #5
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Re: Substitute characters with leading zeros in multiple scenarios

    Thank you martindwilson!! That works perfectly!! Awesome!

    ChemistB and RobertMika I appreciate your efforts as well.

    Just for the record though, ChemistB, the formula doesn't replace the "S" character and
    RobertMika, the formula clips off the last character when there is a "VS" (i.e., while V3817 does correctly become 03817, VS3817 returns 00381)

  6. #6
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Substitute characters with leading zeros in multiple scenarios

    Here is the spreadsheet with my formula used on your values. It's set up to remove up to 6 leading letters of any type.
    Attached Files Attached Files

  7. #7
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Substitute characters with leading zeros in multiple scenarios

    Try this one

    =TEXT(REPLACE(A1,1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))-1,""),"00000")

    A
    B
    1
    V7684 07684 7684 07684
    2
    V366 00366 366 00366
    3
    V88 00088 88 00088
    4
    V60827 60827 60827 60827
    5
    VS57871 57871 57871 57871
    6
    VS67 00067 67 00067
    7
    ABSR123 00123
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. returning first 10 characters of a number including leading zeros'
    By SusanDoyle in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-30-2013, 07:47 AM
  2. Replies: 11
    Last Post: 01-23-2013, 02:04 PM
  3. Leading zeros
    By mary in forum Excel General
    Replies: 1
    Last Post: 01-18-2006, 10:00 AM
  4. Replies: 1
    Last Post: 05-04-2005, 02:06 PM
  5. Leading zeros
    By JC in forum Excel General
    Replies: 9
    Last Post: 02-01-2005, 11:06 AM

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