Forum Discussion
What is the simplest formula to return leading digits from a text string?
- Jul 16, 2022
I'll kick off with
=IFERROR(LOOKUP(9.99999999999999E+307,--LEFT(A2,SEQUENCE(LEN(A2)))),"")
I'll kick off with
=IFERROR(LOOKUP(9.99999999999999E+307,--LEFT(A2,SEQUENCE(LEN(A2)))),"")
- flexyourdataJul 16, 2022Iron Contributor
Ingenious!
It would never have occurred to me to do it that way.
I had to step through it to understand it.
For others, in case it's of interest:
- LEFT(text,SEQUENCE(LEN(text))) creates an array of iteratively one-character longer text substrings from the text
- double-negative on that array converts all non-numbers to errors
- In case the lookup value is not found, the array form of LOOKUP defaults to the largest value less than or equal to the lookup value, and since you've used the largest value possible in Excel as the lookup value, it will always find the value in the --(LEFT...etc) array with the most digits, which will effectively be the longest substring of numbers at the beginning of the stringReally amazing. Thanks!
- restatsJul 31, 2023Copper ContributorThanks for the input.