Multiple Criteria ranges within a Countifs statement - excel

I have criteria ranges (C3:C9, I4:I9, O4:O9) which I would like to use in my countifs statement. Doing one range on its own works as expected. But as soon as I add more than one range into the countif statement it returns a NULL value.
Due to the layout of the spreadsheet these values are the same just split across 3 ranges.
The spreadsheet should help me view times teams are going on lunch. And i have a working model. Just need help working with Multiple Data ranges in Countifs
Tried using a single Data set - Works as expected
Tried all sets singular. They also work as intended.
=COUNTIFS($C4:C$9, "<=" & A12,$E4:$E9,">" & A12) Works Single Criteria Range)
Works Single Criteria Range)
=COUNTIFS($C4:C$9 $I$4:$I$8 $O$4:$O$9, "<=" & A12,$E4:$E9,">" & A12)
NULL Value Does not work.
The Expected output would be for all data ranges to be accepted and not return a null value.

You might want to consider using SUMPRODUCT(), here is an example:
The formula I used translates to:
=SUMPRODUCT(((A1:A11<=A12)*(A1:A11<>"")*(C1:C11>A12))+((D1:D11<=A12)*(D1:D11<>"")*(F1:F11>A12)))
You can extend the formula with more ranges obviously. In your case it would look like:
=SUMPRODUCT(((C4:C9<=A12)*(C4:C9<>"")*(E4:E9>A12))+((I4:I9<=A12)*(I4:I9<>"")*(K4:K9>A12))+((O4:O9<=A12)*(O4:O9<>"")*(Q4:Q9>A12)))

Related

Using SUMIFS with one array criteria and other non array criteria

I am struggling with using the SUMIFS function with one variable array criteria.
=SUM(SUMIFS('Opportunity List'!$AO$3:$AO$711,'Opportunity List'!$AM$3:$AM$711,">="&DATE(YEAR(C137),MONTH(C137),1),'Opportunity List'!$AM$3:$AM$711,"<="&DATE(YEAR(C137),MONTH(C137),31),'Opportunity List'!$K$3:K$711,"{"&F145&"}"))
As you can see, the above formula has two criteria sets that are not arrays and work perfectly, however when I add in the last criteria set nothing happens.
F145 in this case is a string that changes based on check boxes.
Such as ' "Budgetary","FFP" ' or ' "Budgetary","FFP","ROM" '
I have also tried formatting the variable string as a conditional array.
=SUM(SUMIFS('Opportunity List'!$AO$3:$AO$711,'Opportunity List'!$AM$3:$AM$711,">="&DATE(YEAR(C138),MONTH(C138),1),'Opportunity List'!$AM$3:$AM$711,"<="&DATE(YEAR(C138),MONTH(C138),31),'Opportunity List'!$K$3:K$711,PDArray))
PDArray being the array that changes. Still to no avail.
Is Is there a simple syntax error I am missing? Or is there nonfunctionality due to only one of the three criteria sets being an array and the SUMIFS output array is not fully defined?
Thank you in advance!

COUNTIF with date range and multiple criteria

I am trying to simplify a process of counting how many of a specified criteria are in a table.
What I need is to count the number of items that meet all of the following criteria:
[BusinessArea] = "Corporate"
[Application] = "CS"
[Status] = "Resolved"
[ResolvedDate] = *if the resolved date is between DateA and DateB eon a
separate worksheet.
I can do it using VBA or Formulas but I just cannot figure out the date part. I have them figured out separately as:
=IF(AND(Sheet1!I71 >= (TODAY()-7), Sheet1!I71 <TODAY()), TRUE, FALSE)
Where i71 is the [ResolvedDate] (it is searching just this one entry without the other filters.
=COUNTIFS(Table8[Business Area], "Corporate", Table8[Application], "CS")
Where it counts the number of entries that are Corporate_CS entries.
What I currently have:
'=COUNTIFS(Table8[Reported Date],AND(Table8[Reported Date]<='Ticket Summary'!F61, Table8[Reported Date]< TODAY()),Table8[Business Area], "Corporate", Table8[Application], "CS")'
Where F61 is a previous date (beginning of range)
Of which it is returning 9 instead of 6. There are 9 entries that match the criteria, 6 matching the date range and criteria
Try this:
=COUNTIFS(Table8[BusinessArea], "Corporate", Table8[Application], "CS",Table8[Status], "Resolved", Table8[ResolvedDate], ">=" & I61, Table8[ResolvedDate], "< " & I71)
I'm assuming that I61 is the start date and I71 is the end date.
I used this as a guide for the dates: count-cells-between-dates
Multi-criteria counting is a little tough in Excel. I used to make extra columns and concatenate values. Then you can use countif without too much trouble. But it was messy. You could also use DCOUNTA by setting up your query parameters on another part of your sheet (but it doesn't like tables - only cell ranges). Again, its a little messy, but it's very flexible.
My preferred method is to use the SUMPRODUCT function. Everything can be done in one formula and it works with tables. To get your count using SUMPRODUCT:
=SUMPRODUCT((Table8[BusinessArea]="Corporate")*(Table8[Application]="CS")*(Table8[Status]="Resolved")*(Table8[ResolvedDate]>DateA)*(Table8[ResolvedDate]<DateB))

Array Formula To Return List Based on Multiple Conditions

I have a spreadsheet that looks like the picture below. I have some formulas that perform counts on this sheet and compare this data to another data source. An example of one of these would be:
=Countifs(A2:A10676,"0",C2:C10676,"OPEN",D2:D10676,"Current")
How would I return the list of loan numbers associated with the count? For instance, the count above returns 3038. I tried the below formula. I selected C2:C3039, typed the formula and hit Shift + Ctrl + Enter:
=If(And(A2:A10676="0",C2:C10676="OPEN",D2:D10676="Current"),B2:B10676,"")
My thought was that this evaluates the logical for each line and builds an array of the values in column B. When I enter this, each cell in the array is blank.
Can someone please explain how to return an array with values based on multiple criteria?
I see two problems with your formula
=If(And(A2:A10676="0",C2:C10676="OPEN",D2:D10676="Current"),B2:B10676,"")
Firstly you can't use AND to return an array - AND (like OR) returns a single result (TRUE or FALSE), so you need either nested IFs or to use * to simulate AND, i.e. either
=IF(A2:A10676=0,IF(C2:C10676="OPEN",IF(D2:D10676="Current",B2:B10676,"")))
....or.....
=IF((A2:A10676=0)*(C2:C10676="OPEN")*(D2:D10676="Current"),B2:B10676,"")
Note: I used 0 without quotes for the first criteria - for COUNTIFS you can use "0" or 0 but here it needs to match the data type - only use quotes if the data is text formatted - I'm assuming that isn't the case
The second problem is that for both of those the resulting array still has 10675 values because it still includes all the blanks for rows when the criteria aren't met.
To get an array of just 3038 values you can use this formula array entered into the correct sized range:
=INDEX(B2:B10676,SMALL(IF(A2:A10676=0,IF(C2:C10676="OPEN",IF(D2:D10676="Current",ROW(B2:B10676)-ROW(B2)+1)),ROW(INDIRECT("1:"&E2))))
Where E2 contains your COUNTIFS formula
Note that this only works to return an array in worksheet range - it won't work to return an array to be used in another function
Here is an approach that uses a "helper column" rather than an array formula:
In E2 enter the formula:
=IF(AND(A2=0,C2="OPEN",D2="Current"),1+MAX($E$1:E1),"")
and copy down (this marks the multiple rows meeting the criteria)
In F2 enter:
=IFERROR(INDEX(B$2:B$24,MATCH(ROWS($1:1),$E$2:$E$24,0)),"")
and copy down.
You can set a data filter:
Dim wrk As Worksheet
Set wrk = ActiveSheet
If Not wrk.AutoFilterMode Then wrk.range("a1").AutoFilter
wrk.range("a1").AutoFilter field:=1, Criteria1:="0"
wrk.range("a1").AutoFilter field:=3, Criteria1:="OPEN"
wrk.range("a1").AutoFilter field:=4, Criteria1:="Current"
Set wrk = Nothing
Of course, you could manually turn on the filters to how you like, too.
Insert four blank rows at the top, copy A5:D6 into A1 and delete B2. DATA > Sort & Filter, Advanced, Copy to another location, List range: A5:D10680, Criteria range: A1:D2, Copy to: F1:I1, check Unique records only, OK.

Excel - Convert String with if condition to a formula

I'd like to use something like the EVALUATE-Function in Excel for if-statements.
I've got the following issue: I'd like to use Excel to validate my data. I've got three sheets:
the real data I'd like to check. Each row represents a customer and each column some data. The columns have specific names like “age”, “name”, …
the description of the checks I’d like to perform. Each row represents one check and I’ve got 3 columns: 1 check_id – an identifier of each check; 2 check_desc – a description of the check that every normal person can understand like “Age below 18”; 3 rule – the Excel Formula as a string like If(age<18, “error”, “no error”)
the place where sheet 1 and 2 should come together. Each row should represent one customer and each column one check.
Now, if I’ve got for example check_1 “If(age<18, “error”, “no error”)” and the customer data 10 and 20, then the check for the first customer should fire and the check for the second shouldn’t.
If the data is changed, and the age is set from 10 to 18, then everything should be fine, or if the rule is changes to “If(age<21, “error”, “no error”)” then the new condition should be applied to all data.
Is something like this possible?
With the evaluate function only ‘simple’ formulas work.
Thanks in advance,
Martin
Attached you can find the
Excel-Sample File
You will definitely need some VBA here. Make a custom EVAL function:
Public Function EVAL(ByRef Rng As Range, Formula As String) As Variant
Dim RngAddress As String
RngAddress = "'" & Rng.Parent.Name & "'!" & Rng.Address(External:=False)
EVAL = Evaluate(Replace(Formula, "$", RngAddress))
End Function
Then you can easily evaluate your values with formulas passed as text ($ is for parameter):
=EVAL(A1, "IF($<21,""error"",""no error"")")
(note the escaped double quotes). But you would rather pass formula from another cell - then you can specify formula in cell with single quotes:
IF($<21,"error","no error")
=EVAL(A1, B1)
I personally would rename check_desc!B2 to "Check_1" (named range) and then refer to that. You can use the INDIRECT function as well once you've renamed your column header "Check_1" as well.
Note: the value in this case should be "18" instead of "age below 18". You can of course change the number format to "age below "0.
If the relations are changing too I would insert a table that would have uniform formulae changing in each cell when changed in one cell.
The only issue you would then face is the non-expanding nature of your table. You can use VBA for this, but maintaining formulae increases your accountability even if it would be slightly easier not to deal with nasty nested functions.

HLOOKUP in R1C1 notation

I'm using Excel VBA to insert an HLOOKUP function at the end of a given row. I've been attempting to use the R1C1 functionality in VBA for inserting the HLOOKUP function, which should appear as follows in Excel:
=HLOOKUP(D2,'DM NYASSOV'!3:34,32,0)
My issue is that I need the HLOOKUP to be dynamic enough that it can reference variables from the same row on which the HLOOKUP function is to be pasted.
Currently my VBA reads as follows:
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(RC[-17],'DM RC[-19]!'RC[2]:RC[3],RC[3]-RC[2]+1,0)"
RC[-17] references the variable I'm looking for;
RC[-19] is the underlying book/tab identifier;
RC[2] contains the initial row value, and
RC[3] contains the final row reference range.
My main issue is with correctly identifying the dynamic range selection:
'DM NYASSOV'!3:34 / 'DM RC[-19]!'RC[2]:RC[3]
Any pointers are greatly appreciated.
I think you have a few issues here. First is using VBA to dynamically build a formula that then calculates a third value that then gets used in the formula which finally calculates a result. This seems like an unnecessary amount of runaround for a value that can be done with either a formula built on the front end or calculated via VBA in the back end. The second is all the dynamic lookup of it all. It's not so much a problem, but rather a lot to keep track of as you jump through the four hoops. Just the same...
The first parameters of your HLOOKUP can either be a value like "SM1804" or a reference to a cell that contains "SM1804". You can either use VBA to bring this value directly into the formula and save Excel some processing by having to lookup that value at formula processing time:
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(" & Sheets(selectTab).Cells(r, 21).Offset(0, -17).value & ",'DM RC[-19]!'RC[2]:RC[3],RC[3]-RC[2]+1,0)"
Or you can stick the reference to the cell in there (which is what you are doing now):
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(RC[-17],'DM RC[-19]!'RC[2]:RC[3],RC[3]-RC[2]+1,0)"
On to the second parameter... I believe you have a sheet name in DM!RC[-19] This is totally OK, but you'll need to use 'Indirect' to get that sheet name into the HLOOKUP formula:
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(RC[-17],Indirect("'" & DM!RC[-19] & "'!RC[2]:RC[3]"),RC[3]-RC[2]+1,0)"
...this is where things get a little tricky. If, for instance, in DM!RC[-19] you have the sheetname "Sheet1" then indirect is going to return: 'Sheet1'!RC[2]:RC[3] and your HLOOKUP will use that range to do the lookup... Doesn't make a lot of sense to do a HLOOKUP on a range with two cells. So I assume that you have number values in RC[2] and RC[3] there that represent rows. So really the indirect would have to look like:
ThisWorkbook.Sheets(selectTab).Cells(r, 21).FormulaR1C1 = "=HLOOKUP(RC[-17],Indirect("'" & DM!RC[-19] & "'!R" & RC[2] & "C:R" & RC[3] & "C"),RC[3]-RC[2]+1,0)"
Now if your RC[2] has the value "4" and your RC[3] has the value "20" your indirect will return: 'Sheet1'!R4C:R20C and your HLOOKUP will use that value as the range in which it will look up.
You are also doing some math on those values in RC[2] and RC[3], so that's probably all good and doesn't need to be changed. You just need to keep in your head what VBA is going to return as a formula, and then what that formula is going to get from Indirect and then what the resulting HLOOKUP is going to find.
It's a lot to keep track of and may be simplified just by writing the formulas directly in the cell and copying down, or just doing the HLOOKUP functionality directly in VBA.

Resources