+ Reply to Thread
Results 1 to 10 of 10

Removing Leading 0's in string

  1. #1
    Registered User
    Join Date
    10-18-2019
    Location
    Oklahoma
    MS-Off Ver
    2016
    Posts
    1

    Question Removing Leading 0's in string

    So, I have a string like so:

    02-022.auditorium
    22-222.auditorium

    If a first number, or first number after the dash are 0's, I need to remove them to look like:

    2-22.auditorium
    22-222.auditorium

    I have two formulas to remove the zeros but I'm having trouble with the logic to run them both at once. They are (A4 holds the full string 02-022.auditorium):

    Remove the first 0 if exists (yes, I know I could use LEFT but I'd rather keep things as similar as possible)...
    =IF(MID(A4,FIND("-",A4)-2,1)="0",REPLACE(A4,1,1,""),A4)
    DISPLAYS: 2-022.auditorium

    Remove the first 0 after the dash if exists...
    =IF(MID(A4, FIND("-", A4)+1, 1)="0", REPLACE(A4,4,1,""), A4)
    DISPLAYS: 02-22.auditorium

    So... How can I combine these to remove both 0's if they exist?

    Thanks for the help!

  2. #2
    Forum Expert Moo the Dog's Avatar
    Join Date
    09-28-2012
    Location
    Wisconsin
    MS-Off Ver
    Office 365 (PC & Mac)
    Posts
    1,845

    Re: Removing Leading 0's in string

    Couldn't you just use:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    - Moo

  3. #3
    Forum Expert
    Join Date
    09-30-2019
    Location
    Chiangmai, Thailand
    MS-Off Ver
    Office 2016, Excel 2019
    Posts
    1,234

    Re: Removing Leading 0's in string

    I hope that your data pattern is

    nnn-nnnn.xxxxx

    Please Login or Register  to view this content.
    Regards.
    Last edited by menem; 10-20-2019 at 05:08 AM. Reason: Add code tags

  4. #4
    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: Removing Leading 0's in string

    Try this

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    v A B
    1 02-022.auditorium 2-022.auditorium
    2 22-222.auditorium 22-222.auditorium
    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

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,078

    Re: Removing Leading 0's in string

    Maybe
    =SUBSTITUTE(IF(LEFT(A4)="0",MID(A4,2,100),A4),"-0","-")

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Removing Leading 0's in string

    For the OP (and future readers of this thread), the difference between AlKey's and Fluff13's formulas has to do with text that does not have a leading number. Fluff13's formula will return the contents of that cell exactly as is whereas AlKey's formula will return an empty text string ("") if there are no numbers in the text at all or, if there are numbers later on in the text, the text starting at that internal number will be returned. For example, if cell A1 contains "ONE234FIVE", Fluff13's formula will return "ONE234FIVE" whereas AlKey's formula will return "234FIVE". As a further example, if cell A1 contains "NoNumbers" then Flull13's formula will return "NoNumbers" whereas AlKey's formula will return "".

  7. #7
    Valued Forum Contributor Root_'s Avatar
    Join Date
    07-29-2017
    Location
    _
    MS-Off Ver
    2010+
    Posts
    485

    Re: Removing Leading 0's in string

    Another (shorter) maybe :-)

    =REPLACE(SUBSTITUTE("-"&A4,"-0","-"),1,1,"")

  8. #8
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Removing Leading 0's in string

    Quote Originally Posted by Rick Rothstein View Post
    For the OP (and future readers of this thread), the difference between AlKey's and Fluff13's formulas has to do with text that does not have a leading number.
    Another key difference between AlKey's and Fluff13's formulas (beside what I pointed out in Message #6) is that AlKey's formula does not remove leading zeros from the second number (after the dash) which the OP indicated was required whereas Fluff13's formula does do that.



    Quote Originally Posted by Root_ View Post
    Another (shorter) maybe :-)

    =REPLACE(SUBSTITUTE("-"&A4,"-0","-"),1,1,"")
    This formula does not handle trailing zeros correctly (it removes them) so this 12-030.text should become 12-30.text but the above formula returns 12-3.text instead.

  9. #9
    Valued Forum Contributor Root_'s Avatar
    Join Date
    07-29-2017
    Location
    _
    MS-Off Ver
    2010+
    Posts
    485

    Re: Removing Leading 0's in string

    Quote Originally Posted by Rick Rothstein View Post
    ...=REPLACE(SUBSTITUTE("-"&A4,"-0","-"),1,1,"") This formula does not handle trailing zeros correctly (it removes them) so this 12-030.text should become 12-30.text but the above formula returns 12-3.text instead.
    Are you sure? It does return 12-30.text in my Excel 2010 test sheet.

  10. #10
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Removing Leading 0's in string

    Yikes! You are correct... your code does remove the leading zeros. Sorry about that... I thought I had copied your formula when I pasted it for my test and, as it turns out, a different formula got pasted and I did not realize it. That is the good news... now for the possibly bad news. If that second number has two leading zeros, your code only removes the first zero and leaves the second zero. Now, that may be alright... we cannot tell from the limited examples the OP posted, so we will have to wait for the OP to tell us if such number are possible and, if they are, how they should be handled.

+ 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. Replies: 14
    Last Post: 09-26-2017, 02:05 PM
  2. [SOLVED] Removing leading apostrophe VBA
    By nigelbloomy in forum Excel General
    Replies: 0
    Last Post: 09-28-2015, 04:56 PM
  3. Removing leading and trailing / ,
    By araviintl in forum Excel General
    Replies: 6
    Last Post: 05-10-2014, 03:50 AM
  4. [SOLVED] Removing leading and trailing spaces
    By mangesh in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-18-2013, 05:25 AM
  5. Removing a leading space in VB script
    By FkRndmLttrs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-18-2012, 10:33 AM
  6. Removing leading zeros in code
    By zakcat1972 in forum Excel General
    Replies: 3
    Last Post: 02-27-2008, 01:18 PM
  7. Removing leading 0s
    By bnkone in forum Excel General
    Replies: 2
    Last Post: 03-01-2006, 05:00 PM

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