Create Local Range Names in VBA

QB

Board Regular
Joined
Jul 5, 2004
Messages
93
Hi

I need to create local range names in several sheets via VBA. The name for all the named cells will be "TabName" The values stored in the named cells will be used to build a lookup formulae using the INDIRECT function.

Any help would be much appreciated.

Regards

QB
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
One way:

Code:
Sub x()
    Dim wks         As Worksheet
 
    For Each wks In Worksheets
        wks.Names.Add Name:="TabName", RefersTo:=wks.Range("A1:A10")
    Next wks
End Sub
 
Upvote 0
Hi shg

Many thanks for your reply. Your code didn't seem to work for me - it kept producing Global rather than Local named ranges but it did give me a strong steer to a solution. I eventually solved it with the following code:

With Cells(3, 1)
.Name = ActiveSheet.Name & "!" & "TabName"
.Value = sCoAbr
End With

Again, many thanks

QB
 
Upvote 0
FWIW, shg's code worked perfectly for me. You say it kept producing global names, but you can only have one global range with a given name, so how could that be?
 
Upvote 0
Hi Rorya

When I said it kept producing Global Names I should have said that one global name was created and it pointed to a range on the last worksheet in my collection of sheets when the code completed.

Looking more carefully at my attempt to use shg's code, I missed the wks. from wks.Range("A1:A10") when I modified shg's code. Putting that back in worked - local names as expected.

I have suitably chastised mysely - who am I to doubt a Mr Excel MVP!

Regards

QB
 
Upvote 0
He's also a Microsoft MVP... :)
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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