Using a named cell in a macro

P

Pa Maher

Currently in a macro, I copy a cell from Workbook A into Workbook B by
specifying the worksheet and cell ID.
The cell is named. I have tried using the cell name but it's not working.
 
J

JE McGimpsey

Pa Maher said:
Currently in a macro, I copy a cell from Workbook A into Workbook B by
specifying the worksheet and cell ID.
The cell is named. I have tried using the cell name but it's not working.

Is the cell named on the workbook level or the sheet level?

One way:

If the name 'myname' is defined on the workbook-level:

Dim rCopy As Range
Set rCopy = Workbooks("A.xls").Names("myname").RefersToRange
rCopy.Copy _
Destination:=Workbooks("B.xls").Sheets("Sheet1").Range("A1")

Or, if it's a sheet-level name:

Dim rCopy As Range
Set rCopy = Workbooks("A.xls").Sheets("Sheet1").Range("myname")
rCopy.Copy _
Destination:=Workbooks("B.xls").Sheets("Sheet1").Range("A1")
 
B

Bob Greenblatt

Is the cell named on the workbook level or the sheet level?

One way:

If the name 'myname' is defined on the workbook-level:

Dim rCopy As Range
Set rCopy = Workbooks("A.xls").Names("myname").RefersToRange
rCopy.Copy _
Destination:=Workbooks("B.xls").Sheets("Sheet1").Range("A1")

Or, if it's a sheet-level name:

Dim rCopy As Range
Set rCopy = Workbooks("A.xls").Sheets("Sheet1").Range("myname")
rCopy.Copy _
Destination:=Workbooks("B.xls").Sheets("Sheet1").Range("A1")
Or, yet another way that doesn't care whether the name is workbook or sheet
level is:

Set rcopy=workbooks("a.xls").sheets("sheetname").[myname]
 

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