Remove trailing zeros

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,385
Office Version
  1. 365
Platform
  1. Windows
How can I remove the trailing zeros from the inputs below? Everything to the right is what I would like to keep

D000000000 -- D
D0SA000000 -- D0SA
D1A0000000 -- D1A
 
A basic recursive lambda option for MS365 (defined in Name Manager):

RTrimChar
Excel Formula:
=LAMBDA(text,char,IF(RIGHT(text)=char,RTrimChar(LEFT(text,LEN(text)-1),char),text))
Works with one text value at a time. For example:
Excel Formula:
=RTrimChar(A2,"0")
-OR-
Excel Formula:
=MAP(A2:A4,LAMBDA(v,RTrimChar(v,"0")))

You could also set "0" as the default char:

RTrimChar
Excel Formula:
=LAMBDA(text,[char],LET(char,IF(ISOMITTED(char),"0",char),IF(RIGHT(text)=char,RTrimChar(LEFT(text,LEN(text)-1),char),text)))
Which would help to simplify its use with MAP:
Excel Formula:
=MAP(A2:A4,RTrimChar)

But it's also possible to make the custom function accept an array of text strings (without MAP):

RTrimChar
Excel Formula:
=LAMBDA(text,[char],LET(char,IF(ISOMITTED(char),"0",char),test,RIGHT(text)=char,IF(OR(test),RTrimChar(IF(test,TEXTBEFORE(text,char,-1),text),char),text)))
Example:
Excel Formula:
=RTrimChar(A2:A4)

For older versions of Excel, a basic user-defined function in VBA:
VBA Code:
Function RTrimChar(text As String, char As String) As String
    Dim str As String: str = text
    Do Until Right(str, 1) <> char
        str = Left(str, Len(str) - 1)
    Loop
    RTrimChar = str
End Function
 
Upvote 0
Solution
Thank you all for the solutions. I ended up using the UDF which worked great.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Remove trailing zeros.xlsx
AB
1D000000000D
2D0SA000000D0SA
3D1A0000000D1A
Sheet!
Cell Formulas
RangeFormula
B1:B3B1=CONCAT(IF((MID($A1,SEQUENCE(LEN($A1)),1)="0")+(MID($A1&0,SEQUENCE(LEN($A1))+1,1)="0")<>2,MID($A1,SEQUENCE(LEN($A1)),1),""))
 
Upvote 0
With many decades in software development and coding I prefer the KISS principle
 
Upvote 1
A 1-line UDF:

VBA Code:
Function RTrimChar(text As String, char As String) As String
    RTrimChar = Replace(RTrim(Replace(text, char, " ")), " ", char)
End Function
 
Upvote 0
Another option using standard worksheet functions (avoiding REGEX) and also works if less than 3 zeros at the end (re post 4) or if multiple consecutive internal zeros exist (re post 14).
Column B to copy down or column C for spill results.

FryGirl.xlsm
ABC
1DataResult 1Result 2
2D000000000DD
3D0SA000000D0SAD0SA
4D1A0000000D1AD1A
5 
6XYZXYZXYZ
7A0B0A0BA0B
8D00SA000000D00SAD00SA
Sheet1
Cell Formulas
RangeFormula
C2:C8C2=MAP(A2:A8,LAMBDA(r,LET(n,LEN(r)+1,s,SEQUENCE(n),LEFT(r,n-AGGREGATE(14,6,IF(-RIGHT(r&0,s)=0,s),1)))))
B2:B8B2=LET(n,LEN(A2)+1,s,SEQUENCE(n),LEFT(A2,n-AGGREGATE(14,6,IF(-RIGHT(A2&0,s)=0,s),1)))
Dynamic array formulas.
 
Upvote 0
:oops: Just realised that my formulas above would fail for data like row 9 below so revised options in cols D:E

FryGirl.xlsm
ABCDE
1DataResult 1Result 2Result 3Result 4
2D000000000DDDD
3D0SA000000D0SAD0SAD0SAD0SA
4D1A0000000D1AD1AD1AD1A
5  
6XYZXYZXYZXYZXYZ
7A0B0A0BA0BA0BA0B
8D00SA000000D00SAD00SAD00SAD00SA
9D0E000000DDD0ED0E
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=MAP(A2:A9,LAMBDA(r,LET(n,LEN(r)+1,s,SEQUENCE(n),LEFT(r,n-AGGREGATE(14,6,IF(-RIGHT(r&0,s)=0,s),1)))))
E2:E9E2=MAP(A2:A9,LAMBDA(r,LET(n,LEN(r)+1,s,SEQUENCE(n),LEFT(r,n-AGGREGATE(14,6,IF(RIGHT(r&0,s)=REPT(0,s),s),1)))))
B2:B9B2=LET(n,LEN(A2)+1,s,SEQUENCE(n),LEFT(A2,n-AGGREGATE(14,6,IF(-RIGHT(A2&0,s)=0,s),1)))
D2:D9D2=LET(n,LEN(A2)+1,s,SEQUENCE(n),LEFT(A2,n-AGGREGATE(14,6,IF(RIGHT(A2&0,s)=REPT(0,s),s),1)))
Dynamic array formulas.
 
Upvote 0
Book2
AB
1Data
2D000000000D
3D0SA000000D0SA
4D1A0000000D1A
5XYZXYZ
6A0B0A0B
7D00SA000000D00SA
8D0E100000D0E1
Sheet1
Cell Formulas
RangeFormula
B2:B8B2=IFNA(LEFT(A2,XMATCH(TRUE,--(MID(A2,SEQUENCE(LEN(A2)),LEN(A2)))=0)-1),A2)
 
Upvote 0
Hi Scott
Not sure if it would be possible with the OP's data but that fails with the A9 sample data from my previous post or, say, "D50E120" - same reason that I changed from my post 17 formula to that in post 18.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top