Setting name value across workbooks

K

kittronald

In a macro, how can you take the evaluated value of the defined name,
"Name1", in Book1.xlsb and write it to the .Refersto field of the defined
name, "Name2", in Book2.xlsb ?

In Book1.xlsb, the defined name "Name1" has a Refersto field of
=SUM(1,1)

In Book2.xlsb, the defined name "Name2" should have a value of "2"
written to it.



kittronald
 
A

Auric__

kittronald said:
In a macro, how can you take the evaluated value of the defined name,
"Name1", in Book1.xlsb and write it to the .Refersto field of the defined
name, "Name2", in Book2.xlsb ?

In Book1.xlsb, the defined name "Name1" has a Refersto field of
=SUM(1,1)

In Book2.xlsb, the defined name "Name2" should have a value of "2"
written to it.

Evaluate:
MsgBox Evaluate("=SUM(1,1)")
Name2.Value = Evaluate(Name1.Refersto)
 
K

kittronald

After entering the following:

Sub Test()
Workbooks("C:\Temp\Book2.xlsb").Range("Name2").Value =
Application.Evaluate(Workbooks("C:\Temp\Book1.xlsb").Range("Name1").RefersTo)
End Sub

Why is the following error occuring ?

Run-time error '9':

Subscript out of range


kittronald
 
K

kittronald

I've got this working as follows:

Workbooks.Open Filename:="C:\Temp\Book2.xlsb"
Workbooks("Book2.xlsb").Names("Name2").RefersTo =
Application.Evaluate(Workbooks("Book1.xlsb").Names("Name1").RefersTo)
Workbooks("Book2.xlsb").Save
Workbooks("Book2.xlsb").Close

Is there any way to reduce this code ?



kittronald
 
V

Vacuum Sealed

Workbooks.Open Filename:="C:\Temp\Book2.xlsb"
Workbooks("Book2.xlsb").Names("Name2").RefersTo =
Application.Evaluate(Workbooks("Book1.xlsb").Names("Name1").RefersTo)
Workbooks("Book2.xlsb").Save
Workbooks("Book2.xlsb").Close

Try:
Workbooks.Open Filename:="C:\Temp\Book2.xlsb"
Workbooks("Book2.xlsb").Names("Name2").RefersTo =
Application.Evaluate(Workbooks("Book1.xlsb").Names("Name1").RefersTo)
Workbooks("Book2.xlsb").Close NoSave
HTH
Mick.
 
K

kittronald

Mick,

I do need to save the file being closed.

Does the code below save only Book2.xlsb upon closing or does it save
all open files ?

Workbooks("Book2.xlsb").Close Save


kittronald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top