Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default VBA Referencing a Named Cell Range in another Workbook

I have a VBA application (in Workbook2) that reads and changes information
in a Named Cell in a user's Workbook (WorkBook1). The name "UserTotal" has
been Insert/Name/Define defined in Workbook1 as =Sheet1!$A$1.

The VBA code works fine whey I explicitly reference the sheet name in
Workbook1 that the Name is defined as, but does not work if I only refer to
Workbook 2. Since different users may have defined "MyTotal" on different
sheets, is there a way of referring to the Named Cell without being explicit
as to what cell it is on, something like:

MyValue = Workbook1.Range(UserTotal).value
instead of
MyValue = Workbook1.Sheet1.Range(UserTotal).value

or lacking that, is there a simple way of setting another variable equal to
the Wookbook.Worksheet name that a variable is defined as in a way that I
can use that in th code line above?

Any help would be appreciated.

Thanks,

Frank Hayes



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA Referencing a Named Cell Range in another Workbook

Frank,

I guess this is because the range is a property of woeksheets, not
workbooks.

You could try

Evaluate(Workbook1.Names("UserTotal").RefersTo)

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Frank & Pam Hayes" wrote in message
...
I have a VBA application (in Workbook2) that reads and changes information
in a Named Cell in a user's Workbook (WorkBook1). The name "UserTotal"

has
been Insert/Name/Define defined in Workbook1 as =Sheet1!$A$1.

The VBA code works fine whey I explicitly reference the sheet name in
Workbook1 that the Name is defined as, but does not work if I only refer

to
Workbook 2. Since different users may have defined "MyTotal" on different
sheets, is there a way of referring to the Named Cell without being

explicit
as to what cell it is on, something like:

MyValue = Workbook1.Range(UserTotal).value
instead of
MyValue = Workbook1.Sheet1.Range(UserTotal).value

or lacking that, is there a simple way of setting another variable equal

to
the Wookbook.Worksheet name that a variable is defined as in a way that I
can use that in th code line above?

Any help would be appreciated.

Thanks,

Frank Hayes





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default VBA Referencing a Named Cell Range in another Workbook

Frank
Range is a property of a worksheet not a workbook, which
is why missing out the workshet doesn't work.

I use this:
dim rng as range
set rng = workbook1.names("UserTotal").referstoRange

yourValue = rng.value
etc...


You need to be careful with names as they can be defined
at the workbook level or the worksheet level and you can
have the same name at both levels. This can create hard
to find errors. My advice would be to make the names your
code uses unusual so there is less chance of a clash.

cheers
simon

-----Original Message-----
I have a VBA application (in Workbook2) that reads and

changes information
in a Named Cell in a user's Workbook (WorkBook1). The

name "UserTotal" has
been Insert/Name/Define defined in Workbook1 as =Sheet1!

$A$1.

The VBA code works fine whey I explicitly reference the

sheet name in
Workbook1 that the Name is defined as, but does not work

if I only refer to
Workbook 2. Since different users may have

defined "MyTotal" on different
sheets, is there a way of referring to the Named Cell

without being explicit
as to what cell it is on, something like:

MyValue = Workbook1.Range(UserTotal).value
instead of
MyValue = Workbook1.Sheet1.Range(UserTotal).value

or lacking that, is there a simple way of setting another

variable equal to
the Wookbook.Worksheet name that a variable is defined as

in a way that I
can use that in th code line above?

Any help would be appreciated.

Thanks,

Frank Hayes



.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default VBA Referencing a Named Cell Range in another Workbook

Simon,

Thank you for the suggestion. It worked great. However, I am not able to
do it in reverse. How would I write the code to change the value in
"UserTotal" from VBA.

set workbook1.names("UserTotal").value = 500 'did not work

Thanks

Frank



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default VBA Referencing a Named Cell Range in another Workbook

Thanks Bob,

Your suggestion worked. In addition, it allowed me to set the value in the
Named Cell by using

Evaluate(Workbook1.Names("UserTotal").RefersTo) = 500

Thanks,

Frank


"Bob Phillips" wrote in message
...
Frank,

I guess this is because the range is a property of woeksheets, not
workbooks.

You could try

Evaluate(Workbook1.Names("UserTotal").RefersTo)

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Frank & Pam Hayes" wrote in message
...
I have a VBA application (in Workbook2) that reads and changes

information
in a Named Cell in a user's Workbook (WorkBook1). The name "UserTotal"

has
been Insert/Name/Define defined in Workbook1 as =Sheet1!$A$1.

The VBA code works fine whey I explicitly reference the sheet name in
Workbook1 that the Name is defined as, but does not work if I only refer

to
Workbook 2. Since different users may have defined "MyTotal" on

different
sheets, is there a way of referring to the Named Cell without being

explicit
as to what cell it is on, something like:

MyValue = Workbook1.Range(UserTotal).value
instead of
MyValue = Workbook1.Sheet1.Range(UserTotal).value

or lacking that, is there a simple way of setting another variable equal

to
the Wookbook.Worksheet name that a variable is defined as in a way that

I
can use that in th code line above?

Any help would be appreciated.

Thanks,

Frank Hayes









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA Referencing a Named Cell Range in another Workbook

Frank,

Interesting that. I have never tried it myself.

One problem with it is that it doesn't work if the name doesn't already
exist. I use this myself

ActiveWorkbook.Names.Add Name:="UserTotal2", RefersTo:="=$A$1"

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Frank & Pam Hayes" wrote in message
...
Thanks Bob,

Your suggestion worked. In addition, it allowed me to set the value in

the
Named Cell by using

Evaluate(Workbook1.Names("UserTotal").RefersTo) = 500

Thanks,

Frank


"Bob Phillips" wrote in message
...
Frank,

I guess this is because the range is a property of woeksheets, not
workbooks.

You could try

Evaluate(Workbook1.Names("UserTotal").RefersTo)

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Frank & Pam Hayes" wrote in message
...
I have a VBA application (in Workbook2) that reads and changes

information
in a Named Cell in a user's Workbook (WorkBook1). The name

"UserTotal"
has
been Insert/Name/Define defined in Workbook1 as =Sheet1!$A$1.

The VBA code works fine whey I explicitly reference the sheet name in
Workbook1 that the Name is defined as, but does not work if I only

refer
to
Workbook 2. Since different users may have defined "MyTotal" on

different
sheets, is there a way of referring to the Named Cell without being

explicit
as to what cell it is on, something like:

MyValue = Workbook1.Range(UserTotal).value
instead of
MyValue = Workbook1.Sheet1.Range(UserTotal).value

or lacking that, is there a simple way of setting another variable

equal
to
the Wookbook.Worksheet name that a variable is defined as in a way

that
I
can use that in th code line above?

Any help would be appreciated.

Thanks,

Frank Hayes









Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referencing a named range on another sheet Jan B Excel Worksheet Functions 5 April 4th 23 12:38 PM
referencing a 3D Named Range Robert H Excel Worksheet Functions 3 January 14th 08 05:46 PM
Referencing a named range based upon Range name entry in cell Barb Reinhardt Excel Worksheet Functions 14 June 20th 07 07:19 PM
referencing a named range using a variable mark kubicki Excel Programming 0 May 7th 04 12:15 AM
Referencing Named Range in Other Sheet Dan E[_2_] Excel Programming 0 July 11th 03 04:22 PM


All times are GMT +1. The time now is 07:05 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"