Count the number of numbers to the right of the "X"

gsdanger

Board Regular
Joined
Jul 12, 2010
Messages
81
Office Version
  1. 2019
Platform
  1. Windows
Good Day all you Excel gurus (Wish I was one of you...lol)

I have a string containing mostly number, but occasionally, there is are X's included.
I would like to know how can I determine how many numbers occur, to the right of the first "X" in the string, starting from the right of the string.

A string Example: 21X50X16880X1074
I want to determine how many numbers occur to the right of the first occurrence of the "X" in the string, starting from the right side.

In the example, the answer is 4, however, I need to automate this count.

Please assist.

Kind Regards.....gsdanger...
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try...

=LEN(REPLACE(A2,1,FIND("|",SUBSTITUTE(UPPER(A2),"X","|",LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X","")))),""))
 
Upvote 0
Good Day Aladin Akyurek,
Thank you for your prompt and positive reply to my problem.
I copied your formula into my spreadsheet, and it works beautifully....Thank you, Thank you, Thank you.
I will try and understand what the code means, when I have a bit more free time.

Thanks heaps....You are a legend......Kind Regards....gsdanger.
 
Upvote 0
I have a string containing mostly number, but occasionally, there is are X's included.
I would like to know how can I determine how many numbers occur, to the right of the first "X" in the string, starting from the right of the string.

A string Example: 21X50X16880X1074
Assuming your text is never longer than 99 characters (if it is, then make the two 99's larger), then I think this formula might also work for you...

=LEN(TRIM(RIGHT(SUBSTITUTE(UPPER(A1),"X",REPT(" ",99)),99)))

Note: If your text has no X's in it, my formula returns the length of the text. If that is a possibility and you would rather it return 0 instead, then use this formula...

=IF(COUNTIF(A1,"*X*"),LEN(TRIM(RIGHT(SUBSTITUTE(UPPER(A1),"X",REPT(" ",99)),99))),0)

Also note that my formulas do not count trailing spaces (I notice that Aladin's formula does).
 
Last edited:
Upvote 0
Good Day Aladin Akyurek,
Thank you for your prompt and positive reply to my problem.
I copied your formula into my spreadsheet, and it works beautifully....Thank you, Thank you, Thank you.
I will try and understand what the code means, when I have a bit more free time.

Thanks heaps....You are a legend......Kind Regards....gsdanger.

You are welcome.

In

=LEN(REPLACE(A2,1,FIND("|",SUBSTITUTE(UPPER(A2),"X","|",LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X","")))),""))

the
LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X","")) determines how many X instances the target counts.

The
SUBSTITUTE(UPPER(A2),"X","|",LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X","")))
bit substitutes "|" for the last instance of "X" in the target.

The
FIND("|",SUBSTITUTE(UPPER(A2),"X","|",LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X",""))))
bit determines the position of "|", that is, the position of the last "X".

The
REPLACE(A2,1,FIND("|",SUBSTITUTE(UPPER(A2),"X","|",LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X","")))),"")
bit replaces the target from its first char till its last instance of "X", leaving just the part after the last "X".

The surrounding LEN delivers the length of the last part from the previous step, the desired outcome.

Hope this helps.


<strike>
</strike>
 
Upvote 0
Good Day Aladin Akyurek and Rick Rothstein,
Again, thanks for your responses, and Thanks Aladin for attempting to educate me on the formula breakdown...I appreciate it.
Rick, I have switched to your solution (the first one), as I forgot to mention that sometimes there may not be any Xs in the cell, so your formula will accommodate this situation.
Once again, thanks guys, and have a great Xmas and a great new year.....

Kind Regards.....gsdanger
 
Upvote 0
Good Day Aladin Akyurek and Rick Rothstein,
Again, thanks for your responses, and Thanks Aladin for attempting to educate me on the formula breakdown...I appreciate it.
Rick, I have switched to your solution (the first one), as I forgot to mention that sometimes there may not be any Xs in the cell, so your formula will accommodate this situation.
Once again, thanks guys, and have a great Xmas and a great new year.....

Kind Regards.....gsdanger

Just for the record, the lengthy formula is also subject to the same modification...

=
IF(COUNTIF(A2,"*X*"),
LEN(REPLACE(A2,1,FIND("|",SUBSTITUTE(UPPER(A2),"X","|",LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X","")))),"")),A2)

=IF(ISNUMBER(SEARCH("X",A2)),
LEN(REPLACE(A2,1,FIND("|",SUBSTITUTE(UPPER(A2),"X","|",LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X","")))),"")),A2)
<strike></strike>
 
Upvote 0

Forum statistics

Threads
1,215,130
Messages
6,123,220
Members
449,091
Latest member
jeremy_bp001

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