Why does INDIRECT() not work with name references? - excel

As far as my understanding goes, if you have value 5 in H4, there are two ways to reference that cell that both work:
=H4
and
=INDIRECT("H4")
I can't figure out a way to make this work with name references though. For example, if I have some data I'd like to calculate the sum of, I can do this:
=SUM(Main!SomeData)
but the other method...
=SUM(INDIRECT("Main!SomeData"))
suddenly does NOT work, and I get a #REF error. I need to be able to use a named range in my INDIRECT() for something more complex, but I can't wrap my head around why it won't work like this. Can anyone help?

Your formula will work if Main is the name of the workbook (not the worksheet) in which the formula resides:
If the formula resides in a different workbook, (say Book1.xlsx), then both should be open for the formula to work.

Related

Get the value of a name referring to a formula in VBA [duplicate]

I have several workbooks containing calculations and I am making an "automatic overview" combining the data in all those sheets. To find the data I use named ranges. These work fine.
However there is one defined name that does not refer to a range. It is a formula.
What I want is to access the result from that formula (in an open calculations book) from my overview book.
Obviously wb.Names("myNamedFormula").RefersToRange does not work as the name does not refer to a range.
wb.Names("myNamedFormula").Value gives me the formula, but not the result. None of the name's members gets the result.
I tried evaluating the formula using Evaluate(...) but this doesn't work as it should be evaluated in the other workbook.
Does anyone know how to get the result?
Thanks,
ps: I know one possibility is to make the name refer to a range and then do the calculation there, but I don't want that.
I think you will have to use Worksheet.Evaluate rather than Application.evaluate (note that all references in the Named Formula should be fully qualified absolute references). Try this syntax for Evaluate
ansa = Workbooks("Book1.xlsb").Worksheets(1).Evaluate(Workbooks("Book1.xlsb").Names("AddNumbers").RefersTo)
This is a bit of a cludge, but:
Add a formual to a cell in the calculation wb =MyNamedFormula (Lets assume as an example cell A1 on Sheet1)
Add a Name in calculation wb PointsToMyNamedFormula that refers to Sheet1!A1
Then you can reference wb.Names("PointsToMyNamedFormula").RefersToRange.Value1

Iterate between workbooks using INDIRECT

I want to create a summary file where I wish to import the data from cell D11 from some workbooks. For this I try to use INDIRECT to call the workbook O284, where O284 through O296 consists of strings like:
2015-01 (for O284), 2015-02 (for O285 etc)
and where O285 is the sheetname, which is consistent for each workbook hence the $$'s.
=INDIRECT("'C:\Path\["&O284&"]"&$O$285&"'!$D$11")
Running this however does not return a value, but merely a ####. I have all the workbooks opened. I suspect I conduct the INDIRECT snippet wrong, but I can't figure out how. Does anybody have a clue?
Regards,
Btw, is there a way to achieve the same thing as with INDIRECT that work with closed workbooks without using macros? Like a "paste value" or something which you can use in the same way as INDIRECT but that returns a digit, without the formula notation in the cell?
EDIT:
=INDIRECT(CONCATENATE("'C:\path\[";O282;"]";$O$283;"'!D11"))
works for getting the value "D11" from the workbook O282. When I drag this formula out, the next column will reference to workbook P282. However, it still get cell D11. I want it to get the E11 cell from this workbook, and work like this. Does anyone have any idea?
=INDIRECT(CONCATENATE("'C:\";[#Path];"\[";[#WorkbookName];"]";[#SheetName];"'!";ADDRESS(11;COLUMN() - 11)))
I think up code is something that can you want.
Showing #### is sometimes because of cell width.

Using indirect function in an IF statement with quotes

I'm making multiple IF statements that are going to have the same layout. Instead of writing the reference sheet name I'd like to reference a cell for the sheet name.
Also in the interests of laziness I'd like to drag the formula so it changes locations it is looking at on the referenced sheet.
At the moment it looks like this.
=IF(sheet1!O2="","",sheet1!O2)
Simple enough.
However I want to use indirect and I can't write it without getting an error.
Last attempt was
=IF((indirect($B$3))!O2="","",(indirect($B$3))!O2)
where Sheet1 is in the cell B3
Doesn't work.
Any help on the correct syntax would be very appreciated.
You need to concatenate $B$3 and "!O2" to generate "Sheet1!O2" as a string for INDIRECT to work, as below:
=IF(indirect($B$3&"!O2")="","",indirect($B$3&"!O2")

Excel formula returns #value when named

I am working with the following table in Excel:
The following formula in evaluates normally when entered directly into a cell:
=DATE(YEAR(DATEVALUE($A$1)),MONTH(DATEVALUE($A$1)),DAY(INDIRECT(ADDRESS(2,COLUMN()))))
However, when I try to place put this in the named function test and call =test in another cell it returns a #VALUE! error. The best answer I have come up with after researching is that named formulas and the indirect function are not always compatible.
If anyone can shed some light to help explain what I am doing wrong or why I can not put an indirect call inside of a named range I would greatly appreciate it!
Yes, I don't believe INDIRECT will work with a named range - you shouldn't really need such a convoluted formula - try just
=(C$2&$A$1)+0
format as date
Edit: as per comments below, INDIRECT is OK but I don't think COLUMN() is liked in the named range. ROW and COLUMN functions sometimes behave badly because they return "arrays" even when single values, so you need another function like MAX or SUM to convert {2} to 2, e.g.
=DATE(YEAR(DATEVALUE($A$1)),MONTH(DATEVALUE($A$1)),DAY(INDIRECT(ADDRESS(2,MAX(COLUMN())))))
although I think there are shorter methods as I indicated above....

how to get the value of a defined name in another workbook, using vba for excel

I have several workbooks containing calculations and I am making an "automatic overview" combining the data in all those sheets. To find the data I use named ranges. These work fine.
However there is one defined name that does not refer to a range. It is a formula.
What I want is to access the result from that formula (in an open calculations book) from my overview book.
Obviously wb.Names("myNamedFormula").RefersToRange does not work as the name does not refer to a range.
wb.Names("myNamedFormula").Value gives me the formula, but not the result. None of the name's members gets the result.
I tried evaluating the formula using Evaluate(...) but this doesn't work as it should be evaluated in the other workbook.
Does anyone know how to get the result?
Thanks,
ps: I know one possibility is to make the name refer to a range and then do the calculation there, but I don't want that.
I think you will have to use Worksheet.Evaluate rather than Application.evaluate (note that all references in the Named Formula should be fully qualified absolute references). Try this syntax for Evaluate
ansa = Workbooks("Book1.xlsb").Worksheets(1).Evaluate(Workbooks("Book1.xlsb").Names("AddNumbers").RefersTo)
This is a bit of a cludge, but:
Add a formual to a cell in the calculation wb =MyNamedFormula (Lets assume as an example cell A1 on Sheet1)
Add a Name in calculation wb PointsToMyNamedFormula that refers to Sheet1!A1
Then you can reference wb.Names("PointsToMyNamedFormula").RefersToRange.Value1

Resources