How to calculate count of text? - excel

I have put a condition for few cells in an excel as Pass and Fail.
Say there are 10 cells where one can select Pass/Fail. Now i need to calculate number of Pass and Number of Fail selected and the count needs to be displayed in another sheet.
Eg: In the 10 cells, User would have selected 7 Pass and 3 Fail. I need to display this count in another tab as 7 and 3.
Can someone help me with the formula to do this?

It's easy:
=COUNTIF(A1:A10,"Pass")
=COUNTIF(A1:A10,"Fail")

Another option for doing this is to use an array formula.
Something like this:
{=SUM(IF(A1:A10="Pass",1,0))}
Or in R1C1 mode:
{=SUM(IF(R1C1:R10C1="Pass",1,0))}
It is very important to enter these formulas holding down the CNTL+SHFT keys together when hitting ENTER. That's what makes an array formula work.
The advantage with this type of formula is you can make it quite a lot more complex than a simple COUNTIF.

If you want to display it in single cell in different sheet you can use this formula using the concatenate method(&)
'=COUNTIF(Sheet1!A1:A10,"Pass")&" Pass "& COUNTIF(Sheet1!A1:A10,"Fail")&" Fail"
Will result to:
7 Pass 3 Fail
Just change the name of the Sheet or the location of the data, sampled is located in A1 to A10 and on Sheet1
If you want it in different cells,
=COUNTIF(Sheet1!A1:A10,"Pass")
=COUNTIF(Sheet1!A1:A10,"Fail")
Will result to:
7
3
Hope that helps.

The simplest method is to use the COUNTIF function like so:
=COUNTIF(Sheet1!A1:A10,"Pass")
=COUNTIF(Sheet1!A1:A10,"Fail")
This assumes the sheet containing the data is called Sheet1

Related

How to count total items in a column, if header name is provided

Sorry! May be I couldn't give a nice question title. I have following sample data. Obviously in case of actual data it will be different and more...
Data Screenshot
Suppose, I put Pen in D17 cell then expected result will be 6 in E17 cell as in table/DataArray Pen column has 6 items. Count should be 4 if I put T-Shirt in D17 cell and so on....
Believe me, I tried combination of many formulas, search on google but no satisfied solution found. I can achieve it by VBA but I am trying to solve it by using excel formulas only.
You can use this array formula in cell E17:
= SUMPRODUCT(MMULT((B3:K3=D17)+0,TRANSPOSE(NOT(ISBLANK(B4:K13))+0)))
Note since this is an array formula, you must press Ctrl+Shift+Enter instead of just Enter after typing this formula.
This will count non-blanks provided user knows the maximum number of rows to set the Height of the OFFSET() function:
=SUM((OFFSET(B3,1,MATCH(D16,B3:J3,0)-1,10,1)<>"")*1)
to be executed with Ctrl+Shift+Enter. The height set in this formula is 10.
Some of the suggested formulas here look a little like overkill to me.....try this normally entered formula
=SUMPRODUCT((B3:K3=D17)*(B4:K13<>""))
or this will do the same
=SUM((B3:K3=D17)*(B4:K13<>""))
confirmed with CTRL+SHIFT+ENTER

Counting the Amount with Multiplication Condition

I have an Excel List That Looks Like This:
And i need to Count the Amount of Rows that have the Value Finished in Row C. But if T Value in Column B Contains a Semikolon it hase to Count double, if it Contains 2 Semikolons it hase to count 3 and so on...
Her is a Picture to show how the Counting should work.
I will not be able to Add Another Column to the Sheet, so i'm Locking for a Matrix Fomula or something like this.
Till now i tried it with the Countif and Countifs Formula but that didn't work.
Can someone give me a hint how to do thsi or waht Fomula would be the best to use?
For your "Finished" total, use this formula (adjust ranges as necessary to fit your actual data):
=SUMPRODUCT((LEN($B$5:$B$28)-LEN(SUBSTITUTE($B$5:$B$28,";",""))+1)*($C$5:$C$28="Finished"))
For your "Pending" total, if it needs the same treatment, use this formula:
=SUMPRODUCT((LEN($B$5:$B$28)-LEN(SUBSTITUTE($B$5:$B$28,";",""))+1)*($C$5:$C$28="Pending"))
Are you allowed to use/create a new worksheet (other than the one containing the data)? If yes, you can always compute the values you need for this to work in a separate worksheet, then use cell referencing to pull the value back into your original workbook. So, in a Sheet2 (for example), I would have two columns:
PENDING: =IF(Sheet1!$C4="Pending",1+LEN(Sheet1!$B4)-LEN(SUBSTITUTE(Sheet1!$B4,";","")),0)
FINISHED: =IF(Sheet1!$C4="Finished",1+LEN(Sheet1!$B4)-LEN(SUBSTITUTE(Sheet1!$B4,";","")),0)
Fill that formula as far down as needed.
Then, on your original sheet where you would like to enter the sums (I called it Sheet1 in the formulae above, you would do a =SUM(Sheet2!$B$1:$B$25) to populate the Amount Pending cell and =SUM(Sheet2!$A$1:$A$25) for the Amount Finished.
**
If you are not allowed to use a new worksheet, I would say a VB script would be the only other choice.
EDITED
Best solution is the SUMPRODUCT formula offered previously.

COUNTIFS with unique values Excel

I am trying to produce a count of the number of times different strings come up in an Excel table. An example table, currently in SHEET1, would be this:
I have another table in another spreadsheet where I want to indicate, for each letter on the left in Table 1, how many entries for "za", "zc" or "zd" come up on the right. However, I would only like to only consider one entry of each.
The end result, on row B of SHEET2, would have to be something like this:
At the moment I am using a combination of SUM and COUNTIFS to do the job.
More specifically, applied to the example, I am using the following formula:
=SUM(COUNTIFS(Sheet1!A1:A18,Sheet2!$A1,Sheet1!B1:B18,{"za","zc","zd"}))
The formula is doing some of what is intended. However, it is not counting each entry just one time. Instead, its is counting, for each letter on the left, every entry of "za","zc" or "zd". The table that the formula is returning is as follows:
How can I change the formula so that it does what I intend?
Thank you.
My initial thought would be:
=SUM(MIN (1,COUNTIFS(Sheet1!A1:A18,Sheet2!$A1,Sheet1!B1:B18,{"za","zc","zd"}))
but I’m not where I can test if the MIN will apply properly to the COUNTIFS array of results. ;-)
EDITED: The MIN function is taking minimum of 1 or all of the items in the COUNTIFS array, rather than minimum of 1 and each item in the COUNTIFS array, which is what I was afraid of. Using
=MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"za"),1)+MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"zc"),1)+MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"zd"),1)
will gain the desired results. It is a little clunky, but simpler than an array formula. If you want an array formula, you can use:
=SUM(FREQUENCY(IFERROR(MATCH({"za","zc","zd"},(IF(Sheet1!$A$1:$A$18=$A5,Sheet1!$B$1:$B$18)),0),""),IFERROR(MATCH({"za","zc","zd"},(IF(Sheet1!$A$1:$A$18=$A5,Sheet1!$B$1:$B$18)),0),"")))
This uses the FREQUENCY function to take a set of values and see how many items in another set of values fall within each of the data ranges. Since you need text instead of numbers, we use the MATCH function to find out the first time the value occurs in your list, returning "" with the IFERROR function if it doesn't. (We only need the first occurrence since you don't want to know how many occurrences there are). Since it is text, we use the same input for both arguments for FREQUENCY.
Therefore, if you need to change the values you are looking for or the ranges in which you are searching, make sure to change both! Alternately, you could list the values out somewhere, say in F1:F3, and make a named range for this, another one for A1:A18, and another for B1:B18. Your formula would then look something like this:
=SUM(FREQUENCY(IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),""),IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),"")))
Then you need only change your named range definitions and your formulas would update. :-)
NOTE: Since this is an array formula, you must close out of the cell by pressing CTRL+SHIFT+ENTER rather than only ENTER. When you look at the formula bar, you should see
{=SUM(FREQUENCY(IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),""),IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),"")))}
It does NOT work to enter the curly braces yourself. ;-)
You can use this formula at B1 and fill down:
B1:
=SUMPRODUCT(((Sheet1!$A$1:$A$18=A1)*(Sheet1!$B$1:$B$18= {"za","zc","zd"}))/
COUNTIFS(Sheet1!$A$1:$A$18,Sheet1!$A$1:$A$18,Sheet1!$B$1:$B$18,Sheet1!$B$1:$B$18))

IF function - is there a way to avoid repeating formula

Can't believe I don't know this, but is there a way to avoid repeating a formula in an if statement if the logical test is dependent on it?
i.e.
=IF((SUMIFS formula)=0,"",SUMIFs formula)
I want to replace that SUMIFS function in the false scenario with something short that will tell it to just programmatically repeat the formula it originally tested for. Repeating the formula twice has to have detrimental effects on processing speed. Negligible, maybe, but want to go for best-practices here. Thanks.
You can force an error like #DIV/0! and then use IFERROR, e.g.
=IFERROR(1/(1/SUMIFS_formula),"")
You can assign a Name to a formula and use the Name..............See:
Assigning a name to a formula
Relevant excerpt -
For example, let's suppose we frequently use a formula like:
=SUM(A1:A100)-SUM(B1:B100) and this resides in A101 and is copied across many columns on row 101. It would be better in this case to
create a custom formula that does this in each cell on row 101. Here
is how;
1) Select cell A101 (this is vital).
2) Go to Insert>Name>Define and
in the "Names in workbook" box type: SalesLessCosts
3) Now click in
the "Refers to" box and type: =SUM(A1:A100)-SUM(B1:B100) then click
Add.
Now you can replace the formula in cell A101 with: =SalesLessCosts.
You can also copy this across row 101 and it will change its relative
references just as the formula =SUM(A1:A100)-SUM(B1:B100) would. The
reason it does this is all down to the fact we selected A101 before
going to Insert>Name>Define and used relative references in
=SUM(A1:A100)-SUM(B1:B100) when we added it to the "Refers to" box.
If all you need to do is hide zeroes, there is an easy way:
Select all cells where you wish to hide zeroes
Go into Custom Number Formatting
Set format to "General;General;"
The custom formatting has a structure of [positive numbers];[negative numbers];[zeroes]
By making the last part blank you are effectively hiding zeroes, but showing everything else.
The advantage over conditional formatting is that you can use this on any background.
A neat trick which I sometimes use is to hide the cell value completely by using a custom format of ";;;". This way you can put images inside the cells, like the conditional formatting ones, and not see the value at all.
Try using the SUBSTITUTE function like this :
=SUBSTITUTE( VLOOKUP( H4; $D$5:$E$8; 2; 0 ); $H$1; $I$1 )
Here is an example:
Here the formula I don't want to repeat twice is the VLOOKUP function.
The result of VLOOKUP is a string found in another table (ex : "Green").
I want to check if that string matches a specific string value in $H$1 (here, "Yellow").
If it does, SUBSTITUTE replaces it with$I$1 (the error string you want. Here, "FORBIDDEN").
If it doesn't, it displays the VLOOKUP result string (the normal authorized output, like "Green").
This is useful for me because my actual formula is quite long, so I don't want to write it twice.
I also dont want to use two different cells, because I'm already applying this formula on 10 columns, meaning I should add an extra 10 columns to make it work.
In some scenarios, MAX() or MIN() can do a wonderful job.
E.g., something like this:
=IF(SUMIFSformula>0,SUMIFSformula, 0)
Can be shortened to this:
=MAX(0,SUMIFSformula)
The LET formula can be used for this exact scenario. You can define the formula as a variable and then within that same cell you can reference the variable in your formula.
The LET formula format looks like this:
=LET(name,name_value,calculation)
SUMIFS Example
Here's how it would work with your SUMIF example so that you don't have to repeat the formula:
In this screenshot we have an array A1:B7. We want to sum the values (Col B) if the name in ColA is "apple".
For this we have a standard SUMIFS formula of
=SUMIFS(B1:B7,A1:A7,"apple")
The formula is showing in E2. The result is shown in E3.
To put this into the IF statement without having to repeat the formula we can use LET as shown in the screenshot.
We create a variable with the SUMIFS formula as the value of that variable. We then write our IF statement using the variable name instead of rewriting the formula multiple times.
=LET(name,name_value,calculation)
Variable name: sumapples
Variable value: SUMIFS(B1:B7,A1:A7,"apple")
Calculation: IF(sumapples=0,"",sumapples)
Put together in the LET function it looks like this:
=LET(sumapples,SUMIFS(B1:B7,A1:B7,"apple"),IF(sumapples=0,"",sumapples))
This LET function can be used in any Excel formula, and is very useful for shortening long formulas that have repetition.
Optional: Extra complexity
If you want to you can get extra complicated by naming multiple variables.
=LET(name,name_value,name2,name_value2,calculation)
Since Excel 2007, the IFERROR statement does what the OP asked. From the help file:
Description:
Returns a value you specify if a formula evaluates to an error; otherwise, returns the result of the formula. [italics mine]
Syntax:
IFERROR(value, value_if_error)
I've since realised that this was already answered by #barry houdini above.
Here is a hack - depending on whether you are just interested in the displayed value, or whether you need to use the value in another formula:
Put your SUMIF formula in the cell (without the IF part)
Create a conditional formatting rule which sets the font color to the background color when the cell value is 0
And hey presto, you get the desired result.
As I said - it's a hack, but it does prevent the double evaluation.
There is no "clean" solution that I am aware of.

Create a custom function in Excel

This seems like such an obvious thing that excel must have this feature, I just can't find it.
How can I create a custom function without using VBA? (VBA is too big of a hammer and causes security warnings, etc).
For example, I have a spreadsheet with several very complex formulas. Each of these formulas are replicated in several columns. Each column has hundreds of entries, so each one is replicated hundreds of times. If I tweak something then I have to manually fill-down or copy my change from one column to another.
A simple one looks like this:
=(Payment1 - F$12)*12 + ($D21-H21)
But what I'd like to do is:
=MyFunction(f$12,$D21,H21)
And have the actual formula for "MyFunction" written just once someplace.
I've found a few things that come close to giving me what I want. For example, in tables Excel will automatically replicate changes in a formula down the rest of the column saving you the step of manually selecting the range and doing a "Fill Down".
It will also allow relative references off of named cells, which seems equivalent of a user-defined parameter-less functions.
if you can use text to create the formula, then you can define a name to evaluate the function.
In cell A2, create a name EvalAbove, and in Refers To, enter =evaluate(A1)
This way, you can construct a formula
e.g. B1 contains SUM, B2 contains =("="&B1&"(A2:A5)")
and in B3, you can then put =EvalAbove
This means that if you change the formula name in B1, then B2 will change to show the changed formula, and B3 will change to show the result.
Note that this still counts as a macro enabled workbook, but there's no VBA code, just named ranges
You can do this for the example you show if I interpret it correctly.
If not you may be able to rearrange things slightly to conform
your function has three parameters:
The first comes from row 12 of the current column
The second from column D of the current row
The third comes from the column two to the right of the current row
I assume Payment1 is a named variable already?
Set the cursor in say F21 and then define this name
MyFunction =(Payment1 - F$12)*12 + ($D21-H21)
This will set the parameters to come from the places shown
To understand this better switch to RC mode and type the formula as:
=(Payment1 - R12C)*12 + (RC4-RC[+2])
You can now propagate down the formula through the F coloumn
=MyFunction
and it will always use the values in the corresponding F12 column Dxx and column Hxx
If you drag the formula to the next column it will use G12, Dxx and Ixx
If you want to change the formula edit it in the define name space
This is a general exception to the rule that you cannot have non-vba UDFs in Excel. Often in Excel the things you want as 'arguments' to the function are actually in fixed places (rows or columns) that can be addressed relatively.
For example you often want to perform a udf on the cell to the left
So a udf giving the cuberoot of the cell to the left would be a named formula like this:
Cuberoot =(RC[-1])^(1/3)
Or in a1 form set the cursor in B1 and type =(A1)^(1/3)
And Excel will convert it internally to the RC form
For three args - use three columns
It works and does not suffer the volatility issue mentioned about evaluate()
Yes I know this is an old posting but it may help someone with the same issue.
Bob J.

Resources