Convert a range into range name in each worksheet?

Jake_Wrestler

Board Regular
Joined
Sep 3, 2003
Messages
150
I often find myself often going into Insert -> Name -> Define and tediously changing my range names and parameters there. It is awfully time consuming.

Really I am just looking for a method to boost my effieciency.

In a perfect world I imagine a macro where I have two columns, one for the name of the range (i.e., ThompsonJune2001, ThompsonJune2002, MilfordJune2001, MilfordJune2002, etc. You can see how this could be time consuming if I have 4 years of monthly data from 5 or more stores [4 X 12 X 5 = 240 range names!!!]) and the other where I can specify the actual corresponding range (i.e., '6-2001'!$A$1:$AA$48, '6-2002'!$A$1:$AA$48, etc.).

From these two columns of input, the macro would create these named ranges on the fly. Is this macro possible??

If you think of an improvement if you can see what I am doing or any other suggestions for other ways that will help to boost my efficiency in creating name ranges like this.

thanks :pray:
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Have you tried using the Name Box rather than the menus - at least when initially creating them? Sorry if this is obvious - just wondering.
 
Upvote 0
The name box is another way I try (in order to mix it up a bit) but doing that 240 plus times is still murder on the carpal tunnel... :(
 
Upvote 0
Hi Jake,

This has only been tested to a limited extent but it should serve as a starting point for doing what you wish.
Code:
Sub NamesTest()
    Dim rngNames As Range, rngCell As Range
    
    With Sheet1
        Set rngNames = .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
        'the range containing the names
        For Each rngCell In rngNames
            With rngCell
                If NameExists(.Value) Then ThisWorkbook.Names(.Value).Delete
                'if a name already exists then delete it
                ThisWorkbook.Names.Add Name:=.Value, RefersTo:="=" & .Offset(0, 1).Value
            End With
        Next rngCell
    End With
    
End Sub

Function NameExists(strName As String) As Boolean
    Dim nmTest As Name
    On Error Resume Next
    Set nmTest = ThisWorkbook.Names(strName)
    On Error GoTo 0
    If Not nmTest Is Nothing Then NameExists = True
End Function
HTH
 
Upvote 0
THanks Ritchie, unfortunately I get the following error:

Compile error. Variable not defined.

It is referring to "Sheet1" on the third line...

I am assuming to use the macro I select the range with the names in it then run it, is this correct?
 
Upvote 0
Hi Jake,

The "Sheet1" part is the sheet CodeName for the first sheet in a new workbook (which is what I used for testing). It looks as if you have deleted this particular sheet at some stage but that doesn't matter, it was just an example - change it to the CodeName of the sheet containing the names and 'refers to' details in your workbook or replace it with Worksheets("WhateverName"), where "WhateverName" is the name of your chosen sheet .

No need to select the range, the code will pick up the names from column A of the specified sheet.
 
Upvote 0
Hi Richie,

After I created some named ranges in sheet1 and place their names in column A, I get the following error when I run it:

application-defined or object defined error

It is referring to this line:
ThisWorkbook.Names.Add Name:=.Value, RefersTo:="=" & .Offset(0, 1).Value

Maybe I am going about it wrong. I creatd the following named ranges

one =Sheet1!$D$3:$E$9

two =Sheet1!$C$7:$D$15

three =Sheet1!$C$9:$F$9

four =Sheet1!$D$5:$D$24

in sheet1 and put the names (one, two, three, four) in column A then run the macros, is this right? I am assuming it should take those named ranges and populate all the sheets with them, right?
 
Upvote 0
Hi Jake,

Come on, look at these things as a challenge and try to figure out why they don't work :wink:

Specifically, if you look at the line that the error arises on:
Code:
ThisWorkbook.Names.Add Name:=.Value, RefersTo:="=" & .Offset(0, 1).Value
what does it do?

It adds a name to the names collection of ThisWorkbook.
The name is taken from the value of the cell in column A.
The refers to is equal to "=" plus the value in the cell next to the one in column A.

So, if you are adding references with "=" at the beginning in column B then the code is effectively 'seeing double', try ditching one of the "=" from the start, like this:
deleteme.xls
ABCD
1oneSheet1!$D$3:$E$9
2twoSheet1!$C$7:$D$15
3threeSheet1!$C$9:$F$9
4fourSheet1!$D$5:$D$24
Sheet1


or like this:
Code:
ThisWorkbook.Names.Add Name:=.Value, RefersTo:=.Offset(0, 1).Value
HTH
 
Upvote 0
Richie,

I am still getting the same error and I have been fiddling with it for a few hours. :oops: Any ideas? I set up the data like you suggested:
dasak.xls
ABCD
1PL0102PL0102!$A$1:$G$305
2PL0202PL0202!$A$1:$G$305
3PL0302PL0302!$A$1:$G$305
4PL0402PL0402!$A$1:$G$305
5PL0502PL0502!$A$1:$G$305
6PL0602PL0602!$A$1:$G$305
7PL0702PL0702!$A$1:$G$305
8PL0802PL0802!$A$1:$G$305
9PL0902PL0902!$A$1:$G$305
10PL1002PL1002!$A$1:$G$305
11PL1102PL1102!$A$1:$G$305
Sheet1



Thanks Richie
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

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