VBA Name Range not Working

bobo999

New Member
Joined
Feb 25, 2013
Messages
35
Hey Guys. So I've created some code, part of which is designed to name a custom range.

Code is:
Code:
ThisWorkbook.Names.Add Name:="Model2", _
        RefersTo:=Worksheets("Sheet2").Range(range1)

Rather simple, generate the range name, and whatever worksheets we are on at the time, gets that named range. The code works great by itself, in the spreadsheet where the code was originally written.

HOWEVER, when I create a packed custom .xlam file, which I have added as an Add-in into excel, the code doesn't work. I execute the code step by step, get to the point where the code above executes, but it simply doesn't name the range.

Again, when running the code into the original spereadsheet where the code was written, it works, however when opening a new spreadsheet (with appropriate sheet names), the code executes, just doesn't do anything...

Thanks.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Range names inside the Range object must be encased in quote marks just like you do with a cell's address. Try it this way...

Code:
ThisWorkbook.Names.Add Name:="Model2", RefersTo:=Worksheets("Sheet2").Range("range1")
 
Upvote 0
Sorry the code previous is me generating a range like so:

Code:
range1 = "E" & (rowbegin + 1) & ":" & "E" & (rowend + 1)
ThisWorkbook.Names.Add Name:="ImageType", _
    RefersTo:=Worksheets("Virtualization").Range(range1)

It only works without the quotations in that case...
Again it works fine in the original file where I wrote the code, however when I put it in an .xlam addin, and run in a new workbook, it simply doesn't do anything.
 
Last edited:
Upvote 0
To anyone who had a problem with this, the solution:
When running the code "thisworkbook", from the XLAM, excel is looking into the xlam as "thisworkbook". If you want to point the xlam to the workbook from where you are running the code, you must use "activeworkbook". Simple fix.
 
Upvote 0

Forum statistics

Threads
1,202,983
Messages
6,052,903
Members
444,610
Latest member
dodong

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