In our Staff timetable, employees can have an "A"shift (which starts at 9am) a "B" shift (which starts at 10:30am) etc.
I need to know how many shifts in total the employees make so what I use now is a multiple times COUNTIF to count the presents of a few arguments in a range of cells
=countif(D8:BM8;A43)+countif(D8:BM8;A44)+countif(D8:BM8;A45)+countif(D8:BM8;A46)++countif(D8:BM8;A47)
Where field A43 contains "A" field A44 cointains "B" etc.
This works perfect, however, I want to have a smarter way to do this, so I tried to use "COUNTIFS" but the result is always 0 and I can't find why
=COUNTIFS(D8:BM8;A43;D8:BM8;A44;D8:BM8;A45;D8:BM8;A46;D8:BM8;A47)
if looks like all arguments are checked with a logical and, but I need a logical and in this case or a solution with dcounta
You are getting a 0 because there is no cell that will meet ALL conditions.
Instead, maybe try something like
=sum(ArrayFormula(--regexmatch(D8:BM8; textjoin("|"; 1; A43:A47))))
Regexmatch returns a boolean, for all the cells in D8:BM8 (true if a match is found, false otherwise). These booleans are converted into 1 and 0 (see the -- in front of the regex). Then all those values are summed.
Copy and paste the code in a module.(VBA)
Select the cell you want to have the results
Use COUNTIFMATCH Function as the usally functions.
The first Argument is the range you want to count. The second Argument is the range with your criteria. e.g
=COUNTIFMATCH($A$1:$A$20,$C$1:$C$3) or =COUNTIFMATCH($A$1:$A$20,($B$1,$D$1))
whatever you need based on your needs.
Option Explicit
Function COUNTIFMATCH(Range As Range, Criteria As Range) As Integer
'counts the number of cells in one range by the values of another range.
Dim datax As Range
Dim rslt As Range
Dim i As Integer
Set rslt = Range
For Each datax In Criteria
i = i + WorksheetFunction.CountIf(rslt, datax)
COUNTIFMATCH = i
Next datax
End Function
Related
What I'm trying to do is find the name of the person who is ranked number 1 in the table shown below. I have tried =LOOKUP and =VLOOKUP but I get an error saying that a result can't be found, even though it's obviously there. I assume that I'm either using the wrong function or just not using it right.
I tried =VLOOKUP(1;D2:H19;1) and =LOOKUP(1;D2:H19;1) but neither seems to work.
Answer
The following formula should produce the behaviour you desire:
=INDEX(D2:D,MATCH(1,H2:H,0))
Explanation
=VLOOKUP can only be used to find values to the right of a search key. To find values to the left of a search key, use a combination of =INDEX and =MATCH.
The =MATCH function searches a specified range for a specified value and returns the relative position of the value in that range. In this case, the value to search for is 1, the range to search through is H2:H, and the 0 at the end specifies that the range is not sorted in any way.
The =INDEX function returns the contents of a cell within a range having a specified row and column offset. In this case, the range is D2:D, and the row is whichever row is returned by =MATCH. =INDEX could take a third argument if it was desired to specify a row offset as well, but that is not necessary here.
Functions used:
=INDEX
=MATCH
You sort your ascending order based on rank then return your desired data using INDEX() function. Try-
=INDEX(SORT(D2:H500,5,1),1,1)
=vlookup(1,{H2:H19, D2:D19},2)
Since vlookup only searches the 1st column of the input range, to use it, you need to flip the columns by composing a local array: {H2:H19, D2:D19}.
{} means concatenation. , in it means horizontal concatenation. With that, the rank column is now the 1st column in the input of vlookup and now vlookup works.
With our local array, the 2nd column are the names and therefore index should be 2.
Also note the use of comma to separate function inputs.
your VLOOKUP formula should look like:
=VLOOKUP(1, {H2:H19, D2:D19}, 2, 0)
also try just:
=FILTER(D:D; H:H=1)
or:
=SORTN(D:D; 1; 1; H:H; 1)
You can use query (usefull in case of ex aequo)
=query(D2:H,"select D where H=1",0)
I have a row with the following values in each cell from A1 until F1
A,A,A,A,A,A
Then in row 2 I have
A,B,A,A,A,A
Since some columns are empty (no value) I filter the row to get only cells with value:
=filter(A1:F1, A1:F1<>"")
then how can i get a true/false response if all values/strings in the filter array are equal?
Similar answer given here, but that was for a set of arbitrary cells, not a range.
Using range notation:
=COUNTUNIQUE(A1:F1)=1
As implied by the function name, it counts the number of unique values. If there is only one unique value, then we know all the cells are equal. Since COUNTUNIQUE disregards blank values, there is no need to use FILTER first. If you want TRUE for all blanks, change = to <=, as it will return 0 in that case.
If you later decide you do want to consider blank values:
=COLUMNS(UNIQUE(A1:F1, 1))=1
This counts the number of columns returned by UNIQUE(..., 1) (1 means "by column"). If it's 1, then all cells are equal.
I have a spreadsheet with several columns and I want to return a different value based on the value in Column A and if any of the other columns show a true or 1.
For example
If column A has the value "A" and any column B-N is either TRUE or 1 then I want to return "Good" to column O
If column A has the value "B" and any column B-N is either TRUE or 1 then I want to return "Best" to column O
Link to spreadsheet: https://docs.google.com/spreadsheets/d/12k9usKsOgrOUhtW5WBvfY7WB5hbfnTMSPgXtrqcRFjM/edit?usp=sharing
Try this in cell O2 (where your sample sheets only has values of TRUE, FALSE, Y or N in B2:N):
=arrayformula((trim(transpose(query(transpose(iferror(regexreplace(regexreplace(text(B2:N,),"(FALSE)|(N)",),"(Y)|(TRUE)",if(A2:A="A","Good",if(A2:A="B","Best",))),)),,columns(B2:N))))))
Alternative where values in B:N are either 0 or 1:
=arrayformula((trim(transpose(query(transpose(iferror(substitute(substitute(B2:N,0,),1,ifs(A2:A="A","Good",A2:A="B","Best")),)),,columns(B2:N))))))
B2:N is the range of cells to process.
The inner SUBSTITUTE clears all 0 values.
The outer SUBSTITUTE swaps 1 values for a test to see if values in A contain "A" or "B".
IFS does the test and returns either "Good" or "Best".
IFERROR hides any #N/A values down the sheet where the rows are empty.
TRANSPOSE transposes the data for QUERY.
QUERY is used to collapse empty cells (vertically). columns(B2:N) is used in the header part of QUERY. This is a quirk of QUERY, where the header number >= the columns of data, QUERY does the collapse.
TRANSPOSE reverts the dataset to the previous orientation.
TRIM removes leading or trailing space.
ARRAYFORMULA allows the formula to automatically cascade down the sheet, rather than you needing to drag the formula down (like with =IF(AND({B2:N2}>0, A2="A"),"Good", "")).
So, you already have this partial solution. To get a second condition to be met before printing something to your cell, just add an AND() with a new COUNTIF(), comparing A column with A, and then at the else argument, repeat your original IF(), just changing A for B and the output for each case. I will look like this:
=IF(AND(countif(A2;"A");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Good";
IF(AND(countif(A2;"B");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Best";"BAD"))
To use it on every row, just autofill the column O. The row numbers will change accordingly and work on its own.
If you need a new if() statement for a third or fourth case, just repeat it, nesting one IF() inside the other, leaving room for a default error message at the end.
i've the following formula the problem i've is, this isn't working as it should be.
=SUMIFS(E9:14,$I$16:$I,FALSE(),$H$16:$H,G8)
E9:E14 is the part which should be summed up when the checkbox in I16:I = FALSE and if the name matches in H16:H from G8. My problem is I am getting this error
The array arguments of "SUMIFS" vary in size
My question would be, how do I get this exact formula to work? Exactly these areas have to be covered and cannot be changed, otherwise everything else is broken.
EDIT: Added example spreadsheet
https://docs.google.com/spreadsheets/d/1DdTSZAfGTpoeun3k2qqkDMG1jnaUaSz6wgSf2heIIDI/edit?usp=sharing
You need to adjust your ranges. Here's how =SUMIFS() works and then you'll see why you need to adjust the function.
=SUMIFS() looks for ranges and then applies the logic. So when you are telling the function to summarise E9:E14 it interprets it as:
SUM(E9,E10,E11,E12,E13,E14)
provided the following conditions. The conditions will tell the function whether to include each of the components (i.e. E9,...,E14).
Whether a condition is met or not is decided using a simple boolean (true/false) array. This could for example be I9:I14=FALSE which is interpreted as the array
{IF(I9=FALSE),IF(I10=FALSE),...,IF(I14=FALSE)}
resulting in an array similar to this:
{TRUE,TRUE,FALSE,FALSE,FALSE,TRUE}
(assuming the conditions I9, I10 and I14 are met but not the other three. The same is done for the second condition (the values in column H being equal to the value in G8, resulting in another array similar to this:
{TRUE,FALSE,FALSE,TRUE,FALSE,TRUE}
(assuming that only the values in H9, H12 and H14 are equal to G8.
When the function compares the two condition arrays and returns an aggregate array similar to this:
{TRUE,FALSE,FALSE,FALSE,FALSE,TRUE}
because only for the first and the last value the conditions are met. Therefore the =SUM function becomes like this:
SUM(E9,FALSE,FALSE,FALSE,FALSE,E14)
where FALSE = 0 so it returns
=SUM(E9,E14)
Here's where you get into trouble
You try to pass the function conditional arrays that are of a different size to the sum array (E9:E14), in effect asking it to compare apples and the age of your neighbour. What you need to do is to create the calculation you have in column E in a new column in rows 24 down and use that as the sum range in =SUMIFS().
I have a bunch of columns that holds scores like 3-1 1-4 1-0 2-2.
I would like to count all columns that are winning, ie. left number is higher than right one.
I have this formula to know if a column is winning : =LEFT(H2; FIND("-"; H2)-1)-RIGHT(H2; FIND("-"; H2)-1)
What I want to do now is to use this formula within COUNTIF, something along the line :
=COUNTIF(A1:A10; "LEFT(CURRENT_COLUMN; FIND("-"; CURRENT_COLUMN)-1)-RIGHT(CURRENT_COLUMN; FIND("-"; CURRENT_COLUMN)-1)")
Is there any way I can do it with a single formula ?
Complex filtering like this can be done with filter, and then the results can be counted with counta. Example:
=counta(filter(A:A, LEFT(A:A, FIND("-", A:A)-1) > RIGHT(A:A, FIND("-", A:A)-1)))
The first argument of filter is the range to be filtered; the second is a formula based on that range (or another range with the same row count) which returns True or False. The rows where the formula evaluates to True are returned.