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 trai...
HansVogelaar
Nov 20, 2023MVP
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)