retreiving names cell values in VB

dcanham

Active Member
Joined
Jun 7, 2006
Messages
306
I'm having trouble retreiving the value from a named cell on another sheet in the same workbook in VB.

ex:
Workbook name = "Test"
Sheet1 has cell named "INV"
Sheet2 has a macro that needs to retreive the value from the named cell in Sheet1.

** here is the line of code
n_Amount = Workbooks("Test").Worksheets("Sheet1").Cells(Range("[Test]Sheet1!INV").Row, Range("[Test]Sheet1!INV").Column)

** here is the error message I'm getting
run time error '1004' method 'range of object _worksheet' failed

I'm thinking this means it can't find the thing I'm indicating? I don't know why? Similar code works for other value calls?

My question is twofold
1) Why would this line be throwing this error?
2) Is there a better way in VB to retreive a value from a named cell on another sheet??

Thanks in advance for any help.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hie, there

Try this, :


Sub findinv()
n_amount=Workbooks("Test").Names("inv").RefersToRange.Value
End Sub
 
Upvote 0
That did the trick sunnyland!!! Thanks for the excellant response. Though I'm still not sure why the other way didn't work.
 
Upvote 0
Erik

If you have a workbook called Test and a named range INV on Sheet1 then this syntax is valid.
Code:
MsgBox Range("[Test]Sheet1!INV").Value
 
Upvote 0
I just tested again, Norie,
you are right

dcanham, sorry for the wrong information

still wondering what the purpose of this syntax would be ?
by the way: this line WORKS for me
Code:
Workbooks("Test").Worksheets("Sheet1").Cells(Range("[Test]Sheet1!INV").Row, Range("[Test]Sheet1!INV").Column)

REMARK
Cells(Range("[Test]Sheet1!INV").Row, Range("[Test]Sheet1!INV").Column)
is the same as
Range("[Test]Sheet1!INV")
as long as we are on the sheet where the named range belongs to
 
Upvote 0
Erik

I don't think you really gave the wrong info.:)

At first I was skeptical about that syntax too, it's not something I would use/recommend but it does seem to work.

By the way I'm also curious about the OP's syntax too.:)
 
Upvote 0
Hello to you all,

I will stick with dcanham as the syntax doesn't work with me either. I am using Office 2000 Excel Version 9.0.2720.

My apologies with the answer I provided before: I forgot the extension, meaning the code will have raise the error suscript out of range
First of all the syntax:
Code:
MsgBox Workbooks("Test").Worksheets("Sheet1").Cells.Count
****Return an error suscript out of range
modified as this[including the.xls ] it works:
Code:
MsgBox Workbooks("Test.xls").Worksheets("Sheet1").Cells.Count

This:
Code:
MsgBox Workbooks("Test.xls").Worksheets("Sheet1").Range("[Test]Sheet1!INV")
Return a runtime error

That works:
Code:
MsgBox Workbooks("Test.xls").Worksheets("Sheet1").Range("INV")
same as

Code:
Workbooks("Test.xls").Names("inv").RefersToRange.Value
May be it depends on the version of Excel
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,487
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top