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

neXus picture neXus · Jul 19, 2012 · Viewed 8.1k times · Source

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.

Answer

Charles Williams picture Charles Williams · Jul 20, 2012

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)