COUNTIF if values are not in one row - excel

Excel
A B C D E F
1 4 400
2 0
3 700
4 0
5 300
6 0
7 0
8 100
9
In the above Excel table I have values in different cells.
In cell A1 I want to count the number of cells that contain a value <> 0.
Therefore, I tried to go with this formula:
A1 = COUNTIF((D1,E2,F3,D4,D5,F6,F7,E8),0)
However, with this formula I get #VALUE!.
I assume the issue is that the array for the COUNTIF formula is not in one row.
One way of solving the issue would be to make a helper column in which I put all the values in one row but I am wondering if there is another way of doing it without any helper column?

If using a non-continuous range is a must you could use:
=SUM(COUNTIF(INDIRECT({"D1","E2","F3","D4:D5","F6:F7","E8"}),"<>0"))
Note that this is volatile!

This is a problem due to your regional settings.
By default, Excel uses the list separator defined under regional settings in Control Panel. The US English version of Excel uses a comma (,) for list separator by default, while other international versions may use a semicolon (;). Try to put a semicolon between range and condition.
A1 = COUNTIF(D1,E2,F3,D4,D5,F6,F7,E8; 0)

It's a bit more typing but if you are intent on using specific cells:
=SUM(D1<>0,E2<>0,F3<>0,D4<>0,D5<>0,F6<>0,F7<>0,E8<>0)
The D1<>0 test will return TRUE or FALSE. In the SUM() function, Excel treats TRUE as 1, and FALSE as 0. And you don't have to use INDIRECT() which can store up trouble for the future.

Related

Sum of integers in excel

I have a simple query in excel. I would like the sum of numbers in three different sheets into fourth excel sheet. I get an error message when when one cell has NULL value like minus sign.
Sheet1 = 1
Sheet2 = 2
Sheet3 = -
Sheet4 = Sheet1!A1+Sheet2!A1+Sheet3!A1
I would like to have 3 as an answer but I get #Value error message. How should I adjust formula?
If the sheets are contiguous and in order you can use:
=SUM(Sheet1:Sheet3!A1)
Where Sheet1 is the left most and Sheet 3 the right most of the three sheets with Sheet2 between.
If the sheets are not contiguous then you can use:
=SUMPRODUCT(SUMIF(INDIRECT("'"&{"Sheet1","Sheet2","Sheet3"}&"'!A1"),"<>"))
With Office 365 and its dynamic arrays we can simplify the second with:
=SUM(INDIRECT("'"&{"Sheet1","Sheet2","Sheet3"}&"'!A1"))
Check if the null cell value it's in number format. Maybe the "-" is text value so you get that #Value error
So if you want to treat something where taking its value gives an error as 0 (and otherwise just take its value), you can do so with the iferror function. You can then build up your sum as follows. I am not disagreeing with the proposal of using SUM. I am showing an option, and letting you add IFERROR to your repertoire.
=iferror(value(Sheet1!A1),0)+iferror(value(Sheet2!A1),0)+iferror(value(Sheet3!A1),0)

Sum row based on criteria across multiple columns

I have googled for hours, not being able to find a solution to what I need/want. I have an Excel sheet where I want to sum the values in one column based on the criteria that either one of two columns should have a specific value in it. For instance
A B C
1 4 20 7
2 5 100 3
3 100 21 4
4 15 21 4
5 21 24 8
I want to sum the values in C given that at least one of A and B contains a value of less than or equal to 20. Let us assume that A1:A5 is named A, B1:B5 is named B, and C1:C5 is named C (for simplicity). I have tried:
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
which gives me the rows where both columns match summed twice, and
={SUMPRODUCT(C,((A<=20)*(C<=20)))}
which gives me only the rows where both columns match
So far, I have settled for the solution of adding a column D with the lowest value of A and B, but it bugs me so much that I can't do it with formulas.
Any help would be highly appreciated, so thanks in advance. All I have found when googling is the "multiple criteria for same column" problem.
Thanks. That works. Found another one that works, after I figured out that excel does not treat 1 + 1 = 1 as I learnt in discrete mathematics, but as you say, counts the both the trues. Tried instead with:
{=SUM(IF((A<=20)+(B<=20);C;0))}
But I like yours better.
Your problem that it is "summing twice" in this formula
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
is due to addition turning first TRUE plus the second TRUE into 2. It is not actually summing twice, because for any row, if only one condition is met, it would count that row only once.
The solution is to transform either the 1 or the 2 into a 1, using an IF:
={SUMPRODUCT(C,IF((A<=20)+(C<=20))>0, 1, 0)}
That way, each value in column C would only be counted at max once.
Following this site you could build up your SUMPRODUCT() formula like this:
=SUMPRODUCT(C,SIGN((A<=20)+(C<=20)))
So, instead of a nested IF() you control your or condition with the SIGN()function.
hth
If you plan to use a large set of data then it is best to use the array formula:
{=SUM(IF((A1:A5<=20)+(B1:B5<=20),C1:C5,0))}
Obviously adjust the range to suit the data set, however if the whole of each column is to form part of the formula then you can simply adjust to:
{=SUM(IF((A:A<=20)+(B:B<=20),C:C,0))}
This will perform the calculation on all rows of data within the A, B and C columns. With either example remember to press Ctrl + Shift + Enter in order to trigger the array formula (as opposed to typing the { and }).

Reverse order of number pairs and then subtract them

I have this text in an Excel cell in column A: 2 / 12 and want to make this subtraction in another cell in column B: =12-2.
For example, values in column A are:
2 /12
3 / 10
0 / l8
0 / 0
and I want to do this subtraction in column B:
= 12-2
= 10-3
= 18-0
= 0-0
I want to appear only the results ex. 10, 7, 18, 0
How can I do this for multiple cells?
Please try:
=RIGHT(A1,FIND("/",A1))-LEFT(A1,2)
copied down to suit.
Excel has built in Text to columns feature that would probably be useful in this case.
Under data
click Text to columns
selected Delimited
Click Next
Under Delimiters in "Other" enter "/" (without parenthesis) click finish.
That should give you 2 separate columns with your values you should then be able to easily subtract them in an adjacent cell.
This is a little weird for an excel file. I am going to make an assumption that your data in column A is formatted as "Text" because otherwise it would be calculating the value. I also assumed that there is a space between the numbers and the /. So with those assumptions in mind you could have this function in column B to get your result.
This example is for Row 1 of course but you simply copy down for the other rows accordingly
=(RIGHT(A1,LEN(A1)-FIND("/",A1)))-(LEFT(A1,FIND("/",A1)-1))

In Excel 2007, how can I SUMIFS indices of multiple columns from a named range?

I am analysing library statistics relating to loans made by particular user categories. The loan data forms the named range LoansToApril2013. Excel 2007 is quite happy for me to use an index range as the sum range in a SUMIF:
=SUMIF(INDEX(LoansToApril2013,0,3),10,INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6))
Here 10 indicates a specific user category, and this sums loans made to that group from three columns. By "index range" I'm referring to the
INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6)
sum_range value.
However, if I switch to using a SUMIFS to add further criteria, Excel returns a #VALUE error if an index range is used. It will only accept a single index.
=SUMIFS(INDEX(LoansToApril2013,0,4),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
works fine
=SUMIFS(INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
returns #value, and I'm not sure why.
Interestingly,
=SUMIFS(INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,4),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
is also accepted and returns the same as the first one with a single index.
I haven't been able to find any documentation or comments relating to this. Does anyone know if there is an alternative structure that would allow SUMIFS to conditionally sum index values from three columns? I'd rather not use three separate formulae and add them together, though it's possible.
The sumifs formula is modelled after an array formula and comparisons in the sumifs need to be the same size, the last one mimics a single column in the LoansToApril2013 array column 4:4 is column 4.
The second to bottom one is 3 columns wide and the comparison columns are 1 column wide causing the error.
sumifs can't do that, but sumproduct can
Example:
X 1 1 1
Y 2 2 2
Z 3 3 3
starting in A1
the formula =SUMPRODUCT((A1:A3="X")*B1:D3) gives the answer 3, and altering the value X in the formula to Y or Z changes the returned value to the appropriate sum of the lines.
Note that this will not work if you have text in the area - it will return #VALUE!
If you can't avoid the text, then you need an array formula. Using the same example, the formula would be =SUM(IF(A1:A3="X",B1:D3)), and to enter it as an array formula, you need to use CTRL+SHIFT+ENTER to enter the formula - you should notice that excel puts { } around the formula. It treats any text as zero, so it will successfully add up the numbers it finds even if you have text in one of the boxes (e.g. change one of the 1's in the example to be blah and the total will be 2 - the formula will add the two remaining 1s in the line)
The two answers above and a bit of searching allowed me to find a formula that worked. I'll put it here for posterity, because questions with no final outcome are a pain for future readers.
=SUMPRODUCT( (INDEX(LoansToApril2013,0,3)=C4) * (INDEX(LoansToApril2013,0,1)="PTFBL") * INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6))
This totals up values in columns 4-6 of the LoansToApril2013 range, where the value in column 3 equals the value in C4 (a.k.a. "the cell to the left of this one with the formula") AND the value in column 1 is "PTFBL".
Despite appearances, it isn't multiplying anything by anything else. I found an explanation on this page, but basically the asterisks are adding criteria to the function. Note that criteria are enclosed in their own brackets, while the range isn't.
If you want to use names ranges you need to use INDIRECT for the Index commands.
I used that formula to check for conditions in two columns, and then SUM the results in a table which has 12 columns for the months (the column is chosen by a helper cell which is 1 to 12 [L4]).
So you can do if:
Dept (1 column name range [C6]) = Sales [D6];
Region (1 column name range [C3]) = USA [D3];
SUM figures in the 12 column monthly named range table [E7] for that 1 single month [L4] for those people/products/line item
Just copy the formula across your report page which has columns 1-12 for the months and you get a monthly summary report with 2 conditions.
=SUMPRODUCT( (INDEX(INDIRECT($C$6),0,1)=$D$6) * (INDEX(INDIRECT($C$3),0,1)=$D$3) * INDEX(INDIRECT($E7),0,L$4))

Check if some one field is empty

in my Excel document I have 51 fields that I need to check that these fields were not empty/ I am using Excel 2003 and there is maximum 30 argument of function OR:
=IF(OR(ISBLANK(A3)=TRUE;ISBLANK(B3)=TRUE...);"Some empty field";"")
Here is my exemple, but I need to check more fields, how can I do this?
You can do this with a simple COUNTA, ie for 51 cells from A3 to AY3
=IF(COUNTA(A3:AY3)=51,"ok",51-COUNTA(A3:AY3) & "fields are empty")
footnote: =ISBLANK(A3) is the same as =ISBLANK(A3)=TRUE but is simpler
Most likely, the best is the option 4 below
Couple of options:
VBA
insert a row 4 (you will hide it afterwards) and put there these formulas:
A4: =IsBlank(A3)
B4: =OR(A4, IsBlank(B3))
and copy this formula further on, e.g. C4: =OR(B4, IsBlank(C3)), ... then, in the last cell you will have the answer
If you know that the cells should contain numbers of if this just works for you, the formula would be =(A3+0)*(B3+0) .. and you test if the result is 0. The blank plus 0 is 0
Possibly best would be for you to use this:=NOT(ISBLANK(A1))+0 and then multiply the results. ISBLANK + 0 converts the true to 1, that is =IF(NOT((ISBLANK(A3))+0) * (NOT(ISBLANK(b3))+0) ... =0, xxxxx

Resources