Formula doesn't recognize cell reference - Dynamic range - excel

I have a SUMIFS formula with which I am trying to build a dynamic range in order to avoid a circular reference. As it must search and sum through all the range, it must not consider the line where the formula is placed.
Sheet - Example
So I wrote this formula:
SUMIFS((C1:ADDRESS(VALUE(ROW(C6))-1;COLUMN(C1));ADDRESS(VALUE(ROW(C6))+1;COLUMN(C1)):ADDRESS(COUNTA(C:C);COLUMN(C1)));(C1:ADDRESS(VALUE(ROW(C6))-1;COLUMN(A1));ADDRESS(VALUE(ROW(C6))+1;COLUMN(A1)):ADDRESS(COUNTA(C:C);COLUMN(A1)));A6;(B1:ADDRESS(VALUE(ROW(C6))-1;COLUMN(B1));ADDRESS(VALUE(ROW(C6))+1;COLUMN(B1)):ADDRESS(COUNTA(C:C);COLUMN(B1)));"X")
However, after the sum_range it doesn't recognize the cell references. It remains "black". I tried another formula with OFFSET but got the same issue.
Any ideia what is going on?
A quick note, I need a dynamic range because there might be new lines inserted afterwards or people might apply filters, so I cannot use a normal reference.

You cannot have the formula in the same column as the range to sum, in this case it would cause a circular reference.
Use INDEX and two SUMIFS, SUMIFS does not like disjointed ranges and ADDRESS/OFFSET/INDIRECT are volatile:
=SUMIFS($C$1:INDEX(C:C;ROW()-1);$A$1:INDEX(A:A;ROW()-1);A2;$B$1:INDEX(B:B;ROW()-1);B2)+SUMIFS(INDEX(C:C;ROW()+1):INDEX(C:C;MATCH(1E+99;C:C));INDEX(A:A;ROW()+1):INDEX(A:A;MATCH(1E+99;C:C));A2;INDEX(B:B;ROW()+1):INDEX(B:B;MATCH(1E+99;C:C));B2)

Related

SUMIFS where the "sum_range" column is unknown

I have a pile of data on Sheet1
I have a SUMIFS formula on Sheet2
The columns I am summing are named NETPERD1, NETPERD2, NETPERD3.....NETPERD12
I want to write the SUMIFS so that I can easily change which column I am summing.
On Sheet2, in cell $C$4, I will enter NETPERD1 or NETPERD2 etc. and I want my SUMIF to determine which column it should sum.
I think I should be able to do this with IndexMatch but I can't get it to work.
Here is my SUMIFS that works.
I want to replace NETPERD1 with $C$4
=SUMIFS(Sheet1!NETPERD1,Sheet1!ACCTGRPCOD,Perplas!$A15,Sheet1!AUDTORG,Perplas!C$1,Sheet1!FSCSDSG,Perplas!C$2,Sheet1!FSCSYR,Perplas!C$3)
If your named ranges span the entire column, then for the INDEX/MATCH try:
=SUMIFS(INDEX(Sheet1!$A:$Z,,Match(Perplas!C$4,Sheet1!A$1:Z$1,0)),...
changing the Z to your rightmost column.
Try using the indirect function. You can supply indirect with a character string that Excel then reads as part of a cell/range reference.
Working off the code you shared + the fact that you said that you'll put in the name of the range in cell C4:
=SUMIFS(INDIRECT("Sheet1!"&$C$4),Sheet1!ACCTGRPCOD,Perplas!$A15,Sheet1!AUDTORG,Perplas!C$1,Sheet1!FSCSDSG,Perplas!C$2,Sheet1!FSCSYR,Perplas!C$3)
I think that should work? I'm a little confused about how you're able to reference the row name without a named range, so that might cause a wrinkle in this.

Excel array formula with wildcard

I have an array formula that refers to a drop down cell ($AG$7) to determine which cells to evaluate. This works well, however, I need to include an additional item in the drop down which is "All".
When this is selected, I want the array formula to use "*" to return all instances from the array, but i can't get it to work.
This is the formula I'm currently using;
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")=$AG$7),1,0))}
I've tried using
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")="*"&$AG$7),1,0))}
and
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")="*"&$AG$7&"*"),1,0))}
But these don't work.
Does anybody have any ideas?
Thanks
A explicit = comparison cannot use wildcards. COUTIFS and SUMIFS can. As far as I see, you want to count only (conditional sum 1 and 0).
Problem is, COUTIFS and SUMIFS will not deal with INDIRECT ranges. But INDIRECT can and should (because of its volatile bahavior) often replaced by INDEX- MATCH.
So:
=COUNTIFS(tblSkillsMatrix[role],[#role],INDEX(tblSkillsMatrix,,MATCH($V$2,tblSkillsMatrix[#Headers],0)),"*"&$AG$7)
If $AG$7 is empty then it counts independent of the column named in $V$2.
Btw.: Within a table (ListObject) this needs not be entered as a array formula.
This is not 100% replacement of your formula since it not works if $V$2 is empty and so no table column title is given. Your formula will then look at all columns but this is not possible using COUNTIFS where each additional range must have the same number of columns as the criteria_range1 argument. So if $V$2 shall also can be empty, then this will not work.
If so then you could use
{=SUM((tblSkillsMatrix[role]=[#role])*(LEFT(INDIRECT("tblSkillsMatrix["&$V$2&"]"),LEN($AG$7))=$AG$7))}
Advantage: both $V$2 and $AG$7can be empty.
Disadvantage: Volatile behavior of INDIRECT and this formula then must be a array formula even in a ListObject-table. It must be confirmed using Ctrl+Shift+Enter.

Insert a formula in a range of cells via VBA using reference cells

I have a code that loops through and calculates a formula for a column of inputs for a row of dates. It is rather slow looping and writing in VBA so I was hoping to instead paste the formula in the cells instead of the computed value. My plan is to create a range for the results and set the range equal to the formula.
However I am stuck with the formula. The formula uses the previous answer plus other data and I am stuck trying to create the formula for the range so the reference cells move for each cell in the range.
The formula will look like this for cell N7
=IF(M7<>0,(M7-$D7)^(1/30.4),$B7*(1-$C7)^(1/$E7))
The formula will look like this when copied to cell O7
=IF(N7<>0,(N7-$D7)^(1/30.4),$B7*(1-$C7)^(1/$E7))
For O8 it would look like this
=IF(N8<>0,(N8-$D8)^(1/30.4),$B8*(1-$C8)^(1/$E8))
I understand how to loop through each cell and write the formula, but I don't think it would be much faster than calculating it and then writing the answer. I was hoping there is a way to set the range equal to a formula that would populate the correct cell references.
Would ActiveCell.Offset be a solution?
Use relative references:
.FormulaR1C1 = "=IF(RC[-1]<>0,(RC[-1]-RC4)^(1/30.4),RC2*(1-RC3)^(1/RC5))"
You can use this on a range to populate in one go if you need rather than looping through with something like this:
Range("N7:P20").FormulaR1C1 = "=IF(RC[-1]<>0,(RC[-1]-RC4)^(1/30.4),RC2*(1-RC3)^(1/RC5))"
R is row and C is column relative to self negatives have to be in []
You can also do this by using A1 Notation.
Range("N7:O8").Formula = "=IF(M7<>0,(M7-$D7)^(1/30.4),$B7*(1-$C7)^(1/$E7))"
There are advantages using R1C1 Notation but for this one, you better off using this, easier to read. HTH.

Reusing of formula driven result in a if statement in excel

=IFERROR(IF((INDEX(named range1,MATCH(named range2&A1,named range3,0)))<>"",INDEX(named range1,MATCH(named range2&A1,named range3,0)),"-"),"")
Here in this formula I am trying to vertical lookup a value using index-match and checking it within a if statement for blank value.If it is not blank I am using the non-blank value to set it in a given cell.
How can I optimise my formula to reduce performance overhead in excel.
I don't want to use vba for this by storing the result in a variable
The most time consuming bit is the Match(), so avoiding a duplication of the same Match is key. You can
place the Index/Match in a helper cell and then use the formula
=IFERROR(IF(B1<>"",B1,"-"),"")
This way the Index/Match will be calculated only once.
place the Match into a named formula. If you keep your wits about you, named formulas can work with relative cell references. Select the cell where you want the formula to go, then create a named range "NamedRange4" with the formula
=MATCH(named range2&A1,named range3,0)
Then use this formula in the selected cell:
=IFERROR(IF((INDEX(named range1,NamedRange4))<>"",INDEX(named range1,NamedRange4),"-"),"")
The Match will be calculated only once and the result stored in the named range. With relative cell referencing of NamedRange4, the IfError formula can be used in other cells with correct results.

Using INDIRECT() to set dynamic range with VLOOKUP()

I am trying to use INDIRECT() to set a range for a VLOOKUP() function based on a defined numeric value. When stepping through the VLOOKUP() formula, the INDIRECT() function returns what appears to be an acceptable range for my purposes. However, when running the formula, a "#N/A" error is returned and I'm not sure why.
To give a little more flavor, I'm using LEFT() and ADDRESS() to determine the column heading letter based on a MATCH() of a separate value across the top row. Using a fixed offset, the MATCH() functions return the correct column headers and I supply the constants for the range size. Below is my formula:
=VLOOKUP(J2,INDIRECT("$"&LEFT(ADDRESS(1,MATCH($H2,ZAS_Lookup,0)+8,4),1+(MATCH($H2,ZAS_Lookup,0)+8>26))&"$1:$"&LEFT(ADDRESS(1,MATCH($H2,ZAS_Lookup,0)+9,4),1+(MATCH($H2,ZAS_Lookup,0)+9>26))&"$29"),2,TRUE)
As I mentioned, when stepping through to the last step before final calculation, the formula appears as if it will condense down to the following:
=VLOOKUP(5,$DW$1:$DX$29,2,TRUE)
This is not the case, however, since it returns a #N/A. Any idea why? Do I need to use a different function to set the range dynamically?
I realized that I needed to name the tab on which the table resides. It is not enough to simply set the ranges - I also needed to tell Excel which sheet the table was on.

Resources