EXCEL - #VALUE on INDIRECT - excel

I'm getting a #VALUE error on an INDIRECT() and can't figure out why. I've never used this function before, so I'd appreciate some pointers.
I'm constructing a string in a cell. The constructed string has a value of the form [items.xls]items!$A$1:$F$30.
For purposes of this question, suppose the constructed string is in cell $A$1 of Sheet1 in workbook1.xls. in cell $B$1, I have =INDIRECT(A1), which I'd hoped would create the actual reference constructed using cell $A$1 so I could use the reference in a VLOOKUP(), but I can't get that far because of the #VALUE error.
I have the workbook items.xls open in excel at the same time as workbook1.xls.
the spreadsheet items exists in workbook1.
what am I doing wrong ?

Because your reference in cell $A$1 is to a range of cells (and not just one cell), you need enter your =Indirect(A1) formula as an array formula.
Select range Sheet1!$B$1:$G$30. 30 rows, 6 columns; the same size as [items.xls]items!$A$1:$F$30.
Type your formula =INDIRECT(A1) into the formula bar. Instead of pressing Enter, press Ctrl+Shift+Enter to complete your formula.
The formula should look like {=INDIRECT(A1)} in the cell.
Check that none of the cells in range [items.xls]items!$A$1:$F$30 has a #VALUE error. If they do, that error will show on your new sheet as well.
Also double-check your formula that constructs the string in cell A1. If part of that formula is returning a #VALUE error, it will pass it on to the INDIRECT() formula.
You can highlight parts of your formula in the formula bar and press F9 to evaluate just that part of the formula. Just remember to hit ESC to exit the cell afterward.
Another alternative is the Evaluate Formula option on the Formulas part of the Excel ribbon menu.

Related

How to use a cell reference in INDIRECT function

I have several cells that calculate totals from a single cell from multiple worksheets, in this case "Y3" from each of the sheets that are in Week, where n is an integer from 1 through 10:
=SUMPRODUCT(COUNTIF(INDIRECT("'Week"&{1,2,3,4,5,6,7,8,9,10}&"'!Y3"),"W"))
I would like to have a cell in my workbook that contains, A1:
1,2,3,4,5,6,7,9,10
So that way I can update only one cell when I add a worksheet to be counted. I've tried a few things and nothing seems to work.
Your formula : =SUMPRODUCT(COUNTIF(INDIRECT("'Week"&{1,2,3,4,5,6,7,8,9,10}&"'!Y3"),"W"))
This formula only work if you have 10 sheets with named Week1,Week2,Week3….Week10
But if you have less than 10 sheets e.g. Week1,Week2,Week3….Week9, your formula will fail and return "#REF!" error
The formula workaround is wrapping with a IFERROR() and become :
=SUMPRODUCT(IFERROR(COUNTIF(INDIRECT("'Week"&{1,2,3,4,5,6,7,8,9,10}&"'!Y3"),"W"),0))
However, if you wanted the formula acting dynamic and in A1 enter : 1,2,3,4,5,6,7,9,10
But 1,2,3,4,5,6,7,9,10 is a text string, you need converted it to an array. Using FILTERXML() can do the work of which available since Excel 2013
This is an array formula you need to confirm by pressing CTRL+SHIFT+ENTER instead of just ENTER:
=SUM(IFERROR(COUNTIF(INDIRECT("'Week"&FILTERXML("<a><b>"&SUBSTITUTE(A1,",","</b><b>")&"</b></a>","//b")&"'!Y3"),"W"),0))
EDIT 1 :
And,
if you haven't Excel 2013, you could use this longer array (CSE) formula instead
=SUM(IFERROR(COUNTIF(INDIRECT("'Week"&TRIM(MID(SUBSTITUTE(","&A1,",",REPT(" ",99)),ROW(INDIRECT("1:"&LEN(A1)-LEN(SUBSTITUTE(A1,",",))+1))*99,99))&"'!Y3"),"W"),0))
After that,
you can make adjustment to A1 number without changing formula contents
AFAIK you can't do that with a formula if you place the sheet list in a single cell I stand corrected: see bosco_yip's answer! (although you could with a UDF).
But, if you place your sheet list in a column, you can do this (using OFFSET, the list is dynamic, but must be the only data in that column. There are alternatives if that doesn't suit you):
=SUMPRODUCT(COUNTIF(INDIRECT("'Week"&TRANSPOSE(OFFSET(A1,1,0,COUNTA(A:A)-1,1))&"'!Y3"),"W"))
Applying the FILTERXML method, to create a non-array formula
=SUMPRODUCT(COUNTIF(INDIRECT("'Week"&FILTERXML("<a><b>"&SUBSTITUTE(A2,",","</b><b>")&"</b></a>","//b")&"'!Y3"),"W"))

Converting Formulas to Ultimate Cell References [IndexMatch, Offset, Indirect]

I was wondering if it would possible to convert Formulas to the most final cell reference using VBA Macro.
For eg.
Let's say the formula in Cell H2 is as follows
=IFERROR(INDEX($D$2:$D$7,MATCH($F2,$C$2:$C$7,0)),"")
Now let's say the evaluation of this index match function directs us to Cell D3 (ie the Value in Cell F2 was same as that in Cell C3).
So would it be possible to convert this formula to
=D3
As its final evaluation was the value of D3.
To get to this I've tried using
Evaluate(ActiveCell.Formula).Address
The above line of code will work on just plain functions such as simply INDEX MATCH, OFFSET, INDIRECT. But once you add IFERROR it fails to function.
Would it be possible to create a universal macro which would break down all of the formulas within a cell to their most ultimate cell references
Thanks.

Reading and executing formulas from text

I have a problem where I have a need to execute a complex formula saved as text.
I can only find VBA and EVAL() solutions, which cannot be used in my situation.
For example, I have a cell with:
IF(VLOOKUP($A1;Summary!$E:$K;1;TRUE)=$A1;VLOOKUP($A1;Summary!$E:$K;7;TRUE);0)
and I would like a formula in another cell to have the result obtained when running the example formula.
I get a #REF! error when trying INDIRECT()
Edit: Basically, is there a way to run EVALUATE() formula without defining your own formula through "Name manager" that does just that.
Assuming you want to display the result right to the right of the formula text.
Suppose the formulatext is in cell A2.
Select cell B2
Control+F3, Add
Name: EvalFormula
RefersTo: =EVALUATE(A2)
OK your way out
in B2 enter =EvalFormula

Excel 2010 CountIf function only works with debug

I have the following excel function:
=SUM(COUNTIF($B22:$BA22,$BY$4:$BY$49))
The first range B22 - BA22 is filled with formulas that read data from other spreadsheets.
The values displayed in those cells are like "1234Pass", "3456Fail".
The values in the second range, BY4 - BY49, contain all of the possible Fail values. Those are typed in manually. All Cells are formatted as general.
The SUM formula above always displays 0 in the cell. However if I click in the formula bar and press F9, the correct value is displayed.
What am I doing wrong?
Enter the formula as an array formula -- hold down ctrl+shift while you hit enter. Excel will put braces {...} around the formula if you do it correctly

INDEX(INDIRECT("DefinedName"),1)=#REF while INDEX(DefinedName,1) works

In an excel workbook, I'm referencing a defined name from another tab (to get dependent data validation). Accessing the defined name directly works, but accessing it through INDIRECT doesn't.
=INDEX(DefinedName,1) returns the first value of the range
=INDEX(INDIRECT("DefinedName"),1) returns #REF!
I have also tried
=INDEX(INDIRECT("SheetName!DefinedName"),1), but it also returns #REF!
Sample file can be downloaded here.
Thomas,
I hadn't come accross this before.
It appeas that INDIRECT and dynamic range names are incompatible. There is a useful reference here from Dicks blog
Not quite an answer, but it may have something to do with the fact that your named range is returning an array instead of a cell reference (in this case {"VALL";"GENADMIN";"HOSP";"CELLAR"}).
You can replicate the error by removing the named range and replacing it with its actual formula, and then F9'ing the formula:
=INDIRECT(OFFSET(Defaults!$C$1,1,0,COUNTA(Defaults!$C:$C)-1))
=INDIRECT({"VALL";"GENADMIN";"HOSP";"CELLAR"})
Since Excel is expecting some sort of reference to a sheet range, it is failing here since it can't resolve the array to anything specific (pressing F9 again yields ={#REF!;#REF!;#REF!;#REF!}).
The INDEX formula works because it can handle the array reference:
=INDEX(OFFSET(Defaults!$C$1,1,0,COUNTA(Defaults!$C:$C)-1),1)
=INDEX({"VALL";"GENADMIN";"HOSP";"CELLAR"},1)
={"VALL"}
Not an expert, but that's my best crack at it.
This absolutely will work. Maybe they were incompatible in 2012, but they certainly work in 2018. I imagine it would have also worked in 2012 if used correctly.
The INDIRECT function attempts to turn TEXT into a range reference. That TEXT must be a valid reference.
A named range is already a Range Reference. There is no need to get a reference from a reference. This is why the OP example works without using INDIRECT.
A more likely implementation would be if you had the value "DefinedName" from the OP, or any named range in a cell. Then point INDIRECT to that cell where it find plain ol' text. Then it turns that text into a range reference.
You can even use formulas in the INDIRECT to modify the reference. A common approach might be to have a Sheet name in a cell and then use INDIRECT to pull data from various sheets. This is primarily useful when you want to copy and paste formulas and have the sheet (or cell) reference dynamically adjust to a new target.
Setup
Start with a blank workbook which should contain three blank worksheets, (Sheet1, Sheet2, and Sheet3) If not, get to that point. Then enter the following basics:
Sheet 1
Cell A1, enter "Range Reference"
Cell A2, enter "SUM from Sheet"
Cell B1 enter "Sheet2!A1:A4" (Note there is no "=" This text is a range reference.)
Cell C1, enter "THIRD_SHEET" (This text is equivalent to a named range we'll create in a second.)
Then in Sheet2,
Cell A1, enter 1
Cell A2, enter 2
Cell A3, enter 3
Cell A4, enter 4
In Sheet3
Cells A1-A4 enter 100 through 400 respectively.
Then create a named range called "THIRD_SHEET" which refers to cells A1-A4 on Sheet3.
Try
In Sheet1:
Cell B2, enter the formula:
"=SUM(INDIRECT(B1))"
Copy that formula to Cell C2.
Results:
The INDIRECT uses the text in the column headings to point to a valid range reference. This is just illustration. You wouldn't likely use a mixture of named ranges and text ranges, and its not very clean to write out, "Sheet1!A1:A4" but you can use any of the standard formula to arrive at a textual reference.
Try
Change the formula in B2:
"=SUM(INDIRECT("Sheet"&COLUMN()))&"!A1:A4"))"
Now copy that to cell C2.
Results
Well, are the same. The SUM from cells A1:A4 is being returned from Sheets 2 and 3 respectively. This time, however, the column headers are not being used, and the range reference is being assembled in the INDIRECT call itself using the string join "&" operator and the COLUMN number.
Named ranges can be used in the same way. What's important to understand is that anything inside the INDIRECT parenthesis needs to first be resolved to a vaild TEXT range reference.
I hope that helps!

Resources