Forum Discussion
DrExcel_Excel_MVP
Nov 20, 2023Copper Contributor
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
- Patrick2788Silver Contributor
This is my variant:
=MAP(Strings,Strip) 'Strip =LAMBDA(str, IF( AND(RIGHT(str) <> "0", LEFT(str) <> "0"), str, Strip(REPLACE(str, IF(RIGHT(str) = "0", LEN(str), 1), 1, "")) ) )
- Riny_van_EekelenPlatinum Contributor
Patrick2788 Perhaps we should leave it here, The OP doesn't seem to be the least bit interested in this discussion after all.
- PeterBartholomew1Silver Contributor
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.
- peiyezhuBronze Contributor=WEBSERVICE("http://e.anyoupin.cn/eh3/?regreplace~^0+|0+$~~" & A2)
- LorenzoSilver Contributor
Power Query for the record
// Table let Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content], Fixed = Table.ReplaceValue(Source, each [Product code], "0", (x,y,z)=> Text.Trim(y,z), {"Product code"} ) in Fixed
(x,y,z)=> Text.Trim(y, z) shall work, isn't it?
- LorenzoSilver Contributor
Good point SergeiBaklan + Thanks. Previous post revised
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_EekelenPlatinum Contributor
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".
- PeterBartholomew1Silver Contributor
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) )
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)