Forum Discussion

DrExcel_Excel_MVP's avatar
DrExcel_Excel_MVP
Copper Contributor
Nov 20, 2023

Leading and Trailing Zero in Excel

How to Remove Leading and Trailing Zeros in Excel 

 

We have a set of product codes like this : 000P2I290002M900

we want to make it like this  P2I290002M9 which means removing all leading and trailing zeros from the code 

 

All my attempts to solve this 

 

 

=MAP(

    B6:B14,

    LAMBDA(x,

        LET(

            a, SUBSTITUTE(x, 0, ""),

            b, MID(x, SEQUENCE(LEN(x)), 1),

            MID(

                LEFT(x, XMATCH(RIGHT(a), b, , -1)),

                MATCH(LEFT(a), b, 0),99))))

 

 

 

=MAP(

    B6:B14,

    LAMBDA(x,

        LET(

            a, TEXTSPLIT(x, , 0),

            TEXTJOIN(0, 0,DROP(

              DROP(a, MATCH(FALSE, a = "", 0) - 1),

              XMATCH(FALSE, a = "", , -1) - COUNTA(a))))))

 

               

 

=MAP(

    B6:B14,

    LAMBDA(x,

        LET(

            a, SUBSTITUTE(x, 0, ""),

            b, SEQUENCE(LEN(x)),

            s, HSTACK(LEFT(a), RIGHT(a)),

            t, MID(x, b, 1),

            f, TOCOL(FIND(s, t) + b - 1, 3),

            MID(x, MIN(f), MAX(f) - MIN(f) + 1))))

 

 

 

 =MAP(

    B6:B14,

    LAMBDA(x,

        LET(

            a, SUBSTITUTE(x, 0, ""),

            b, FIND(LEFT(a), x),

            MID(x,b, MAX(TOCOL(

           FIND(RIGHT(a), MID(x, b, LEN(x)),

           SEQUENCE(LEN(x))),3))))))

          

                       

                   

 

 

=MAP(

    B6:B14,

    LAMBDA(x,

        LET(

            a, TEXTSPLIT(x, , 0),

            b, XMATCH(FALSE, a = "", 0, 1),

            TEXTJOIN(0,0,CHOOSEROWS(a,

             SEQUENCE(XMATCH(FALSE, a = "",

             0, -1) - b + 1, , b))))))

 

My LinkedIn :

https://www.linkedin.com/in/hazemhassandrexcel

My YouTube Channel :
https://www.youtube.com/DroosHROnline

 

#Hazem_Hassan #Dr_Excel #excel #excelskills #exceltricks #exceltips

 

               

 

    • Riny_van_Eekelen's avatar
      Riny_van_Eekelen
      Platinum Contributor

      Patrick2788  Perhaps we should leave it here, The OP doesn't seem to be the least bit interested in this discussion after all.

      • PeterBartholomew1's avatar
        PeterBartholomew1
        Silver Contributor

        Riny_van_Eekelen 

        Maybe it doesn't matter.  The mission of the group appears to be:

        "Your community for how-to discussions and sharing best practices on Microsoft Excel.

        If you’re looking for technical support, please visit Microsoft Answers"

         

        In which case, the fact that I find your answers to be of interest is as relevant as satisfying the OP requirements.  In this specific case, I guess the OP could engage to a greater extent.

         

  • DrExcel_Excel_MVP 

    OfficeScript for the collection

    
    function main(workbook: ExcelScript.Workbook) {
    
      const sheet = workbook
        .getActiveWorksheet()
    
      sheet
        .getRange("C6:C14")
        .setValues(
          sheet
            .getRange("B6:B14")
            .getValues()
            .map(x => [(x + '').replace(/^0+|0+$/g, "")])
        )
    }
    
  • Riny_van_Eekelen's avatar
    Riny_van_Eekelen
    Platinum Contributor

    DrExcel_Excel_MVP 

    Wouldn't this one work? Similar to PeterBartholomew1 's solution though using BYROW.

     

     

    =BYROW(
        myData,
        LAMBDA(a,
            LET(
                keep, MID(a, SEQUENCE(LEN(a)), 1) <>
                    "0",
                first, XMATCH(TRUE, keep, 0, 1),
                last, XMATCH(TRUE, keep, 0, -1),
                return, MID(a, first, last - first + 1),
                return
            )
        )
    )

     

     

    ....where the product codes are in the named range "myData".

     

  • DrExcel_Excel_MVP 

    Hi.  There are similarities.

    My formula came out as

    = MAP(list, TrimZeroλ)

    where TrimZeroλ is given by

    = LET(
        nonzero, Explodeλ(text) <> "0",
        first,   XMATCH(TRUE, nonzero),
        last,    XMATCH(TRUE, nonzero, , -1),
        MID(text, first, 1 + last - first)
      )

    Explodeλ is pretty standard

    = LET(
        n, LEN(text), 
        k, SEQUENCE(n), 
        MID(text, k, 1)
      )
  • DrExcel_Excel_MVP 

    A VBA solution:

    Function RemoveLeadingAndTrailingZeros(s As String) As String
        Dim i As Long
        Dim r As String
        r = s
        Do While Left(r, 1) = "0"
            r = Mid(r, 2)
        Loop
        Do While Right(r, 1) = "0"
            r = Left(r, Len(r) - 1)
        Loop
        RemoveLeadingAndTrailingZeros = r
    End Function

    Use like this:

    =RemoveLeadingAndTrailingZeros(B6)

Resources