Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access value (content) of a named cell in Excel VBA

This

Dim WB As Workbook
Set WB = Workbooks.Open("foo\bar.xlsx", , True)
Debug.Print (WB.Names("named_cell").Value)
WB.Close (False)

returns

='Sheet'!$C$377

instead of the value in the cell C377.

How can I receive the value of the cell instead?

like image 237
speendo Avatar asked May 11 '16 12:05

speendo


2 Answers

If WB.Names("named_cell") returns a Name object, then this object has a RefersToRange property. That returns the Range object referred to by a Name object. See https://msdn.microsoft.com/en-us/library/office/ff834918.aspx.

So

Debug.Print WB.Names("named_cell").RefersToRange.Value

See also Malfunction of Excel-VBA own function when open other excel-file.

like image 157
Axel Richter Avatar answered Nov 14 '22 21:11

Axel Richter


Couple of ways:

Debug.Print Range("Named_Range").Value
Debug.Print [Named_Range].Value '// This uses the evaluate method

You don't need to qualify a named range with a sheet object if it's in the active workbook (which it should be as it's the last workbook you opened).

like image 28
SierraOscar Avatar answered Nov 14 '22 22:11

SierraOscar