Count the number of occurences in an Excel data range - excel

My problem is that I am trying to count the number of sub-categories of column A in column B in excel but not sure how. For example, I want to get the number 2 for category a (a1 & a2), number 5 for category b (b1 & b2 & b3 & b4 & b5) and number 1 for category c (c1).

Here is one method to accomplish what you are looking to do:
**Make sure that there is a blank line above your first row of data (your first "a" and "a1" will need to start in row 2) to keep this simple and make this example work.
Once your data has been shifted per the above, enter this in cell C2 and copy it down as far as your data goes:
=IF(IFERROR(MATCH(B2,B$1:B1,0),"unique")<>"unique","","unique")
Then, to sum the number of distinct categories, enter the following wherever you like:
=COUNTIFS(A:A,"a",C:C,"unique")
=COUNTIFS(A:A,"b",C:C,"unique")
=COUNTIFS(A:A,"c",C:C,"unique")
This can be expanded as needed.
Cheers.

Related

Counting past occurrence given a classification

I have a Table just like the one in the picture below. I want to count the number of cells in the column Leg since the last entry, given a certain classification on the column Type.
For example, for the type KFT only, how many blank entries are there in the column Legendary since the last entry for Leg?
I know how to solve this issue creating helper columns. But given that I have too many types, I would have to create a helper column for each type, making my table too big given the amount of rows I have. Is there any way I can create a VBA function to perform this task for me, such that I only need one cell per type to let me know the amount of blank cells since last Leg entry for each type? Here is a short example of how my table looks, using only 2 types:
In this example, I need a formula with output 0 for MSG, since it has been 0 entries since the last Leg for MSG, and output 2 for UNG, since it has been 2 entries since the last Leg for UNG.
Thanks so much for the help!
Solution 1 Using single scary formula
Alright then. I think best solution to your problem is using helper column or VBA but if you really want to use just one formula, I've something for you. This formula is very very scary and someone might give you better solution also, but this formula does work.
In the below formula I've assumed data to be in range A2:F100. Change 100 to you number of rows of data. (change all 100's, best way will be to copy formula without = in an empty sheet and then FIND 100 and REPLACE ALL to your row number)
If you have listed Types in Column H starting from Cell H2 then in Cell I2 enter the following formul, drag/copy down as required.
=COUNTIFS(INDIRECT("F" & INDEX($A$2:$A$100,MAX(IF(($B$2:$B$100=H2)*($F$2:$F$100=1),ROW($B$2:$B$100)-ROW(INDEX($B$2:$B$100,1,1))+1)))+1 & ":F" & IF(INDEX($A$2:$A$100,MAX(IF(($B$2:$B$100=H2)*($F$2:$F$100=1),ROW($B$2:$B$100)-ROW(INDEX($B$2:$B$100,1,1))+1)))+1>INDEX($A$2:$A$100,MAX(IF(($B$2:$B$100=H2)*($F$2:$F$100=""),ROW($B$2:$B$100)-ROW(INDEX($B$2:$B$100,1,1))+1)))+1,100,INDEX($A$2:$A$100,MAX(IF(($B$2:$B$100=H2)*($F$2:$F$100=""),ROW($B$2:$B$100)-ROW(INDEX($B$2:$B$100,1,1))+1)))+1)),"",INDIRECT("B" & INDEX($A$2:$A$100,MAX(IF(($B$2:$B$100=H2)*($F$2:$F$100=1),ROW($B$2:$B$100)-ROW(INDEX($B$2:$B$100,1,1))+1)))+1 & ":B" & IF(INDEX($A$2:$A$100,MAX(IF(($B$2:$B$100=H2)*($F$2:$F$100=1),ROW($B$2:$B$100)-ROW(INDEX($B$2:$B$100,1,1))+1)))+1>INDEX($A$2:$A$100,MAX(IF(($B$2:$B$100=H2)*($F$2:$F$100=""),ROW($B$2:$B$100)-ROW(INDEX($B$2:$B$100,1,1))+1)))+1,100,INDEX($A$2:$A$100,MAX(IF(($B$2:$B$100=H2)*($F$2:$F$100=""),ROW($B$2:$B$100)-ROW(INDEX($B$2:$B$100,1,1))+1)))+1)),H2)
This is an array formula so commit it by pressing Ctrl+Shift+Enter.
Solution 2 Using Helper Columns
Assuming you listed all Types in Column H starting from Cell H2 then in Cell I2 enter the following array formula to get the last row from data for corresponding Type in Column H and Leg =1
=INDEX($A$2:$A$11,MAX(IF(($B$2:$B$11=H2)*($F$2:$F$11=1),ROW($B$2:$B$11)-ROW(INDEX($B$2:$B$11,1,1))+1)))+1
Then in Cell J2, enter the below array formula to get the last row from data for corresponding Type in Column H and Leg ="" (blank)
=INDEX($A$2:$A$11,MAX(IF(($B$2:$B$11=H2)*($F$2:$F$11=""),ROW($B$2:$B$11)-ROW(INDEX($B$2:$B$11,1,1))+1)))+1
Next, in Cell K2, enter the below formula to set last row if there's no row corresponding to particular type for which leg is blank (for example in my sample data ABC do not have empty Leg)
=IF(I2>J2,11,J2)
Finally, in Cell E2 enter the following formula to get Count
=COUNTIFS(INDIRECT("F" & I2 & ":F" & K2),"",INDIRECT("B" & I2 & ":B" & K2),H2)

If A1 = A2, and B1>B2 then hide Row 2. What formulas/rules to use and where?

In Excel I have a problem...I don't know many formulas/rules.
This is my case:
Scenario 1)
A B C
1.| Drw....3350......03
2.| Drw....3350......02
This is my desire:
I want to hide row 2.
Scenario 2)
A B C
1.| Drw....3350........03
2.| Img.....3350........02
This is my desire:
I want to keep both rows.
My problem in words:
Scenario 1: Because A1 & A2 + B1 & B2 are the same, I want to hide the row with the smallest number in column C.
Scenario 2: Because A1 & A2 are different I want to keep both rows, even though B1 & B2 are the same.
This is a simplified example. Column A could consist of hundreds of rows with mixed values. However, Column A is text and column B and C are values.
There will not be such a case when C1 & C2 are the same. (only if someone typed in wrong)
What I am asking:
I don't know what formulas or rules to use in Excel and would prefer to not use VBA for this. Seems like such a simple task but I just don't know how to.
Anyone?
In your Title you mention and B1>B2 and then give two examples in both of which the 'B' values are the same, so assuming you mean to hide the lower C value where the A values match and the B values match then in a helper column, say E:
=A2&B2
and in another column (I chose H):
=AND(MIN(IF(E7=E:E,C:C))=C7,MAX(IF(E7=E:E,C:C))<>C7)
entered with Ctrl+Shift+Enter and copied down to suit. Then filter that column to select for FALSE:

Search columns and return number of cells containing "5" against colleague name

What I'm looking for is a formula that will do the following: (I'll be using the first row for this example):
Firstly check cells E:J for cells containing a 5.
If all cells contain a 5 it will check the person's name and then add this to the box in column B against the correct name.
If cells E:J have less than 5 in any of these cells it will disregard this row altogether.
To clarify, for Shaun it will count how many rows next to his name have 5's across the board then put the total number of rows fitting this criterion next to his name in Column B.
We can do this with a helper column:
Helper column, keep the name if the count of 5 equals to 5.
Enter below to K2 and fill down.
=IF(COUNTIF(E2:J2,5)=5,D2,"-")
Then we are counting how many times name appears in the helper column. Enter below to B2 and fill down.
=COUNTIF($K$2:$K$11,A2)
Create formula in K2 (output the name if all values are 5)
=IF( AND(E2=5,G2=5,H2=5,I2=5,J2=5), D2, "")
Create the formula at B2which will sum all occurrences of the names
=SUM(IF(K2:K11=A2,1,0))
(and press ctrl+shift+enter)
You maybe will need to replace , with ; (depends on your excel version).

Need to compare part of text in a range of cells to see if exists in another range of cells

Basically... I have 800+ names in columns B and C. I have another 700,000+ names in column A. So I want to check A1 to see if part of the text is found anywhere in B and C and then output the answer to D1.
For example... A1 has the name mike trout in it. Somewhere in b or c could be the name trout. I want D1 to output TRUE because a cell in b or c contained part of the text found in A1. If the names Mike or Trout are nowhere to be found in columns b or c, I want D1 to say FALSE. Can this be done? I've been searching online for hours and cannot come up with the correct formula that works. Thanks.
This is a slightly more complex version of this answer: https://stackoverflow.com/a/22754109/293078. Both use COUNTIF with wildcards. This one requires a helper column. Here's how it looks:
Column B has a formula that adds a space before and after the corresponding name in column A. The formula in B2 is:
=" " & A2 & " "
This gives means that we'll only get matches for full words, e.g., "Ted" won't match "Teddy Bear," but "Teddy" will.
As you described, columns C and D contain the names to search for. Finally, in E2, we have this array formula, which must be entered with ctrl-shift-enter:
=SUM(COUNTIF(B2,"* " & $C$2:$D$8 &" *"))>0
Drag the formulas in B2 and E2 down 700,000 rows and watch the magic happen.
I thought this would be hopelessly slow, but I just tried 700,000 rows and it finished in a few seconds.

Excel: search if a specific text exists in a column

I have two columns. Each cell in column A contains a full sentences and each cell in column B contains a word or phrase. I would like to check if the contents of each cell in column B appears in one of the cells in column A---it could appear in multiple cells in column A or in no cells. The output just needs to be a yes or no (and should be spit out in column C) for my purposes, but it would be neat to return the number of times each column B word came up somewhere in Column A.
So far I haven't figured out how to take a discrete string of letters (already printed in one cell) and search across a range in a column. Not sure if this is beyond the regular excel functionality.
Thanks very much for your help!
Use array formula like this:
=SUM(IF(ISERROR(SEARCH(B1,A:A,1)),0,1))
enter in formula bar then press CTRL+SHIFT+ENTER.
Hope this helps.
Put formula in C.
Try This :
=countif(a:a,"*" & b2 & "*")>0 gives you result in True/Flase
To get the occurrence
=countif(a:a,"*" & b2 & "*")
To get YES/NO
=if(countif(a:a,"*" & b2 & "*")>0,"YES","NO")

Resources