Counting number of leading zeros in cell in Excel - excel

Trying to figure out how to count the number of leading zeros in an excel cell. The value within the cell can be comprised of both numbers and letters or just numbers or just letters. The column is formatted as a text column.
So far I have been able to use
=MIN(FIND({1,2,3,4,5,6,7,8,9},A1&"123456789"))-1
This gets me all the leading zeros correctly for cells containing only numbers but when it is a combination of both digits and letters it also counts the letters.
eg. 00012 = 3 (correct) 000ab = 5 (should be 3)
Is there a way that I can adapt this formula to not count letters?

Try:
=SEARCH(LEFT(SUBSTITUTE(TEXT(A1,"#"),"0",""),1),A1)-1

Try looking for the first digit/character that isn't a zero and subtract 1.
=AGGREGATE(15, 7, ROW($1:$9)/(MID(A2, ROW($1:$9), 1)<>"0"), 1)-1

Related

Counting Odd and Even numbers in a comma-separated list

I have a list of comma-separated numbers (positive,single or double digits) in A1.
The list can only contain up to 20 numbers, can be less but not more.
Is there a formula to count the even and odd numbers in the cell without separating the list in to different cells? Thanks.
For Even:
=SUMPRODUCT(--ISEVEN(--MID(SUBSTITUTE(A1,",",REPT(" ",99)),(ROW($XFD$1:INDEX($XFD:$XFD,LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))-1)*99+1,99)))
Odd:
=SUMPRODUCT(--ISODD(--MID(SUBSTITUTE(A1,",",REPT(" ",99)),(ROW($XFD$1:INDEX($XFD:$XFD,LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))-1)*99+1,99)))
The quantity of odd numbers (put this in cell B1):
=SUMPRODUCT(MOD(MID(SUBSTITUTE(A1,",",REPT(" ",LEN(A1))),1+LEN(A1)*(ROW(INDIRECT("1:"&LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))-1),LEN(A1)),2))
The quantity of even numbers (references the result of the previous formula, which is assumed to be in cell B1):
=LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1-B1

Total occurrences of a single digit from a string of digits across a range of cells in a column

We need to count how many occurrences of each number are in a cell over a range of cells in the same column and output a tally of the totals for each number. See the attached picture and the desired output in the column next to it. We tried other formulas found online in both excel and open office with no results.
letter Count
Working Count
Try the following formula in D1:
=LEN(TEXTJOIN("",TRUE,A:A,"#"))-LEN(SUBSTITUTE(TEXTJOIN("",TRUE,A:A,"#"),C1,""))
and populate down.
(you will need 2016 or later for TEXTJOIN)
Option 1
Single array formula (ctrl+shift+enter !) which will work for strings with a maximum length of [5] alphanumeric characters (but you can easily modify the formula by adding a few numbers in the hard-coded array constant {1,2,3,4,5}):
{=SUM(N(MID($A$1:$A$500,{1,2,3,4,5},1)=TEXT(C3,"#")))}
You can add some further trickery to let Excel define the array constant, so the formula will work for any length of the string of digits :
{=SUM(N(MID($A$1:$A$500,
COLUMN(INDIRECT("A1:"&CHAR(65+MAX(LEN($A$1:$A$500)))&"1"))
,1)=TEXT(C3,"#")))}
The part in the middle (COLUMN()) creates the {1,2,3,4,5} array. You might have seen other versions of this formula, without the CHAR, which I use to create a reference to e.g. cell E1 (knowing that 65 is the code for "A").
.
Option 2
This array formula (ctrl+shift+enter !) works in all Excel versions, but is not very "elegant" as you have to repeat the key part of the formula as many times as the maximum digits you have in your cells (this example is for max 3 characters):
{=SUM(
N(MID($A$1:$A$500;1;1)=TEXT(C3;"#"))+
N(MID($A$1:$A$500;2;1)=TEXT(C3;"#"))+
N(MID($A$1:$A$500;3;1)=TEXT(C3;"#")) )}
The character you are counting is in C3. This will work for numbers and letters. If you can have five alphanumeric characters, you have to add two more N(...) parts, and replace the second parameter of the MID function with 4 and 5 respectively.

SUM numbers, but when 0, add 8 instead

I'm trying to write a SUM(IF(zero, add 8), A1:G1) kind of excel function.
What it should do:
If the cell contains a number, add that number to the sum.
If the cell contains a number = 0, add 8 to the sum.
if the cell is empty, add nothing to the sum
I have tried using SUMIF, but it only "adds the numbers if they meet a criteria", and I rather need "add this when the criteria is met"
You could just make a longer formula:
=SUM(A1:G1)+COUNTIF(A1:G1, 0)*8
The first sum will only do your numbers ignore blanks and 0's, the second part counts the number of 0's and multiplies it by 8.
You can do this with the following formula:
=SUMIF(A1:G1,">0")+8*COUNTIFS(A1:G1,0,A1:G1,"<>")
should be self-explaining :)
Method 1
If you can spare another column, just use an IF statement to replace all 0's by 8's and blanks by 0's (not really necessary) in a new column
=IF(A1=0,8,A1) and take a SUM of this column.
Method 2
Just SUM over your original data, count the number of zeros in your data and multiply this number by 8.
=SUM(A1:A10)+8*COUNTIF(A1:A10,"=0")
Note: The data is in A1:A10 in this case.

Length of Decimal Places Including Trailing Zeros

I'd like to return in one cell the number of decimal places from another cell and I can't seem to figure out a formula to include the trailing zeros so that 1.50 returns 2 instead of 1.
Here's what I have:
=IF(LEN(AF443)-LEN(INT(AF443)) = 0, 0, LEN(AF443)-LEN(INT(AF443))-1)
Not all the decimal places are the same and some have to keep zeros for consistency (e.g. 9.00).
Assuming your number is in cell B2 try this formula:
=RIGHT(CELL("format",B2),-1+LEN(CELL("format",B2)))

Excel Macro to Replace last four digits if containing specific number

I have a spreadsheet with 2000 rows. My concern is with column B which contains 17 digit numbers. I'd like to have a macro check all numbers in column B and if the last four digits = "0000" I'd like to change the last four digits to "0001", also if the last four digits = "1900" I'd like to change the last four digits to "1998". Any other scenario can be skipped.
Thanks
You can use this:
=LEFT(B1,13)&IF(RIGHT(B1,4)="0000","0001",IF(RIGHT(B1,4)="1900","1998",RIGHT(B1,4)))
Just place this in another column and afterwards replace column B with the values from this column.
You can use formula for the same. Following is the excel formula for the same:
=IF(RIGHT(B2,4)="0000",CONCATENATE(MID(B2,1,LEN(B2)-4),"0001"),IF(RIGHT(B2,4)="1900",CONCATENATE(MID(B2,1,LEN(B2)-4),"1998"),B2))
Another alternative:
=IF(RIGHT(B1,4)="0000",B1+1,IF(RIGHT(B1,4)="1900",B1+98,B1))

Resources