PDA

View Full Version : [SOLVED:] Create Named Ranges from worksheet list



oam
08-07-2023, 02:11 PM
I am needing a VBA code that will create Named Ranges for the entire workbook using example information (not a complete listing but is in the same layout) shown below. Also, the SCOPE should be workbook. I have been working with several codes from the web but I can't :banghead: get the codes to create functional Named Ranges.

First all, is it possible to create Named Ranges for the entire workbook? If not, is there an arrangement of the data on the worksheet list (like below) to allow a code to work?
If it is possible, could you provide a code to build all Named Ranges for each sheet in the workbook.

Columns
A B C



Name
Sheet Name
Starting Range


_rng11
Parts
D2:500


_rng15
Parts
S2:S500


_rng29
Orders
AJ2:AJ500


_rng40
Orders
P2:P500


_rng44
Sales
AE2:AE500


_rng5
Sales
B2:B500




Thank you for your time and any and all help

Aussiebear
08-07-2023, 07:19 PM
Have a look here and see if this helps you.https://excelchamps.com/vba/named-range/#VBA_to_Create_Named_Range_from_Selection

p45cal
08-08-2023, 05:11 AM
Something along the lines of:
Sub blah()
For Each cll In Sheets("Sheet1").Range("A2:A7")
ActiveWorkbook.Names.Add Name:=cll.Value, RefersTo:="='" & cll.Offset(, 1).Value & "'!" & cll.Offset(, 2).Value
'ActiveWorkbook.Names.Add Name:=cll.Value, RefersTo:="='" & cll.Offset(, 1).Value & "'!" & Range(cll.Offset(, 2).Value).Address
Next cll
End Sub

Notes
Your first address (D2:500) isn't a valid address
You might want to make the addresses absolute (add $s : $D$2:$D$500) otherwise they'll move depending on the active cell.
(The commented-out line could be used instead to make the addresses absolute)

oam
08-08-2023, 08:27 AM
p45cal and Aussiebear,

Thank you for a quick reply!
p45cal,
Your code worked great and I thank you for your help. I also saw your notes and noticed after running your code is the absolutes were added to all the ranges, is this automatic or is in the code?

Again,
Thank you for your help it was great!

p45cal
08-08-2023, 10:05 AM
after running your code is the absolutes were added to all the ranges, is this automatic or is in the code?If you run the code in msg#3 above as-is, and the addresses in column C are as in your first messge (relative), then the named ranges will have relative addresses (the same as in column C). In fact, the addresses will be exactly as the addresses in column C, so you can use that to pick and choose.

If you comment-in the commented-out code line, and delete or comment-out the first ActiveWorkbook.Names.Add line, you will get absolute addresses in the named ranges, whether or not the addresses in column C are absolute or relative. This is because the .Address property is absolute by default.

oam
08-08-2023, 10:36 AM
p45cal,

Thank you for your explanation of the absolute address, I have added the absolute to each address as you recommended.

Thank you for all your help with this matter!