Substitute all letters for "" until first number from the right

cdalgorta

Board Regular
Joined
Jun 5, 2022
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi,
So basically I have 4 main possibilities:
PYMT769291
INV 769291
INV 769291_1 -> This could be _2 or _3 or _4...
INV 769291BD

And I need 1 formula that makes them all convert into this:
PYMT769291 -> This one should never change
769291 -> This one gets rid of the "INV "
769291 -> This one gets rid of the "INV " & everything after "_"
769291 -> This one gets rid of the "INV " & the 2 letters at the end

So far I have this:
=IFERROR(LEFT(SUBSTITUTE(A1,"INV ",""),FIND("_",SUBSTITUTE(A1,"INV ",""))-1),SUBSTITUTE(A1,"INV ",""))
And I get the following:
PYMT769291
769291
769291
769291BD

I do not know what formula to use to delete the letters at the end of the last one "without" changing the other 3 which are already as I want them to show. Basically a formula that will only delete letters on the right up to the first number it finds, and if it doesn't find any letters, then it gives back the original. Any ideas? I'm a novice, so apologies if my question is too easy for you hahah.

Thank you very much in advance!
Cristian
1658006264460.png
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Is the number that you want to retrieve from the INV cells always 6 digits long?
 
Upvote 0
Give this formula a try...
Excel Formula:
=IF(LEFT(A1,4)="PYMT",A1,LOOKUP(9.9E+307,--LEFT(MID(A1,5,9),ROW($1:$99))))
 
Upvote 0
Solution
Give this formula a try...
Excel Formula:
=IF(LEFT(A1,4)="PYMT",A1,LOOKUP(9.9E+307,--LEFT(MID(A1,5,9),ROW($1:$99))))
Actually, since you are using XL2021, this would be better...
Excel Formula:
=IF(LEFT(A1,4)="PYMT",A1,LOOKUP(9.9E+307,--LEFT(MID(A1,5,9),SEQUENCE(99))))
 
Upvote 0
Give this formula a try...
Excel Formula:
=IF(LEFT(A1,4)="PYMT",A1,LOOKUP(9.9E+307,--LEFT(MID(A1,5,9),ROW($1:$99))))
To me, you are a genius hahah. It worked perfectly. Thank you so much Rick! You are a the real MVP!

Could I kindly ask you to explain to me how this formula works though? So I can learn hahah
What does "LOOKUP(9.9E+307..." do?
And what is the "ROW($1:$99)" for? The data that I ran for testing has over 2000 rows and this "ROW($1:$99)" still works. What does it do?
Thank you in advance!
1658008639004.png
 
Upvote 0
Actually, since you are using XL2021, this would be better...
Excel Formula:
=IF(LEFT(A1,4)="PYMT",A1,LOOKUP(9.9E+307,--LEFT(MID(A1,5,9),SEQUENCE(99))))
Thank you for reminding me that I had to change that hahah. I have 365 actually. Just changed my profile. No idea what the difference is yet, but based on this reply, seems like there are some formulas that are better to use depending on the Excel you have. Good to know hahah.
 
Upvote 0

Forum statistics

Threads
1,213,513
Messages
6,114,072
Members
448,546
Latest member
KH Consulting

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