Count Number of digits 0-9 to the left of a Specific Character in a Cell

MacIndy

New Member
Joined
Dec 30, 2019
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
I used a formula to parse a field that typically contains text like the following:
1F5H or 13F1H

The following formula works just fine when the number of digits to the left of a given character is static, like 1 numerical digit. However, I'm not sure how to determine how many numerical digits there are without making my formula way to complicated.

The following formula works perfect but is currently only setup for 1 numerical digit to the left of "H" or "F"
=IF(AND(ISNUMBER(SEARCH("H",T3)),ISNUMBER(SEARCH("F",T3))),MID(T3,FIND("F",T3)-1,1)*201+MID(T3,FIND("H",T3)-1,1)*138+R3,IF(ISNUMBER(SEARCH("H",T3)),MID(T3,FIND("H",T3)-1,1)*138+R3,IF(ISNUMBER(SEARCH("F",T3)),MID(T3,FIND("F",T3)-1,1)*201+R3,IF(U3>0,U3*100+R3,IF(V3>0,V3*50+R3,IF(W3>0,W3+R3,0))))))

There might be a simpler way of doing this formula too. Grateful for any help. Thank you
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Can you tell us what you want it to do? I am not going to try and reverse engineer your existing formula. You description does not go any further than "parse a field."
 
Upvote 0
Something like this?
This is an array formula and must be entered with CTRL-SHIFT-ENTER

Book1
AB
11F5H2
213F1H3
31H34Y1
412JI5K6Hl64
Sheet1
Cell Formulas
RangeFormula
B1:B4B1{=COUNT(--(MID(A1,ROW(INDIRECT("1:"&SEARCH("H",A1)-1)),1)))}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
A way using Power Query.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Count = Table.AddColumn(Source, "Count", each Text.Length(Text.Select(Text.Middle([Code],0,Text.PositionOfAny([Code],{"H","F"})),{"0".."9"})))
in
    Count
 
Upvote 0
For any amount of numbers to the left of the first any alphabetical.
VBA Code:
=MIN(FIND({"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},UPPER(A1)&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))-1
 
Upvote 0
Here is a small change to my formula in post #3 that will eliminate the need to use CTRL-SHIFT-ENTER.

Book1
AB
11F5H2
213F1H3
31H34Y1
412JI5K6Hl64
Sheet2
Cell Formulas
RangeFormula
B1:B4B1=SUMPRODUCT(--ISNUMBER(--MID(A1,ROW(INDIRECT("1:"&SEARCH("H",A1)-1)),1)))
 
Upvote 0
This formula:

=AGGREGATE(14,6,MID(T3&"0h",SEARCH("h",T3&"0h")-{3,2,1},{3,2,1})*138,1)

should give you the number in front of an h (times 138) as long as it is 3 digits or less. It returns 0 if there is not an h in the cell. Change the {3,2,1} array constants if you want it to look for more digits. There are some cases where this technique might give bad results, but if your data is always number,letter,number,letter, etc. you should never have a problem. Adapting that formula for the "f" value should be easy. Then you can combine the 2 formulas like this:

=IFERROR(1/(1/(AGGREGATE(14,6,MID(T3&"0h",SEARCH("h",T3&"0h")-{3,2,1},{3,2,1})*138,1)+AGGREGATE(14,6,MID(T3&"0f",SEARCH("f",T3&"0f")-{3,2,1},{3,2,1})*201,1))),IF(U3>0,U3*100,IF(V3>0,V3*50,IF(W3>0,W3,-R3))))+R3

which should give you the same results as your original formula, except that it looks for more digits. There might be room for more improvement, since the U3:W3 range is adjacent. I'll look at that some more.
 
Upvote 0
This formula:

=AGGREGATE(14,6,MID(T3&"0h",SEARCH("h",T3&"0h")-{3,2,1},{3,2,1})*138,1)

should give you the number in front of an h (times 138) as long as it is 3 digits or less. It returns 0 if there is not an h in the cell. Change the {3,2,1} array constants if you want it to look for more digits. There are some cases where this technique might give bad results, but if your data is always number,letter,number,letter, etc. you should never have a problem. Adapting that formula for the "f" value should be easy. Then you can combine the 2 formulas like this:

=IFERROR(1/(1/(AGGREGATE(14,6,MID(T3&"0h",SEARCH("h",T3&"0h")-{3,2,1},{3,2,1})*138,1)+AGGREGATE(14,6,MID(T3&"0f",SEARCH("f",T3&"0f")-{3,2,1},{3,2,1})*201,1))),IF(U3>0,U3*100,IF(V3>0,V3*50,IF(W3>0,W3,-R3))))+R3

which should give you the same results as your original formula, except that it looks for more digits. There might be room for more improvement, since the U3:W3 range is adjacent. I'll look at that some more.

This looks like it would work beautifully. Thank you
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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