naming a range of cells on a different spreadsheet

slay0r

Board Regular
Joined
Jul 8, 2011
Messages
231
Hi Guys,

Sorry for asking for so much help in the last couple of days. Doing a side project for work that I hope will get me some brownie points. At the same time it's a learning curve for me! I've just managed to fix something with Cindy so I Can open a side spreadsheet that's readonly. I need to name all the cells below as Import, I need to do the same for another tab and name it export as well later on. Am I doing it right below or have I gone a bit off to the side?

Sub nameimportlist21()
'
' nameimportlist21 Macro
' Macro recorded 12/07/2011 by jmartin
'
'
Windows("Credit-Stop List.xls").Activate
Range("A1:I240").Select
Application.Goto Reference:="import"
Windows("copytestmacromaster.xls").Activate
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi slay0r,

It is not actually necessary to select or activate the windows, sheets, or ranges involved in order to name a range. In your case you can simply do this:

Code:
Sub nameimportlist21()
'
' nameimportlist21 Macro
' Macro recorded 12/07/2011 by jmartin
'
'
Workbooks("Credit-Stop List.xls").ActiveSheet.Range("A1:I240").Name = "import"

End Sub

You can also explicitly name the worksheet instead of having to have the sheet of interest active:

Workbooks("Credit-Stop List.xls").Worksheets("Sheet2").Range("A1:I240").Name = "import"

Keep Excelling.

Damon
 
Upvote 0
Hi Jonathan,

You can add or redefine a named range with Workbook scope using the code below.
The code assumes Credit-Stop List.xls is already open.

Rich (BB code):
Sub DefineName_Import()
    With Workbooks("Credit-Stop List.xls")
        .Names.Add ("Import"), _
            RefersTo:=.Sheets("Sheet1").Range("A1:I240")
    End With
End Sub

Your post didn't list the sheet name for the range - you can edit Sheet1 with your sheet's name.

Note that you don't have to Activate the window or workbook to modify it.

P.S. Oops. Sorry- I had this on my screen for a while and didn't realize Damon had responded.
(Fortunately for me my response wasn't too different) ;)
 
Last edited:
Upvote 0
Thanks very much for the help guys. That's worked a treat. Didn't realise you could do it without activating so thank you very much for that tip too!

Kind Regards
Jonathan
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

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