VBA for creating many named ranges

DutchKevin

Board Regular
Joined
Apr 13, 2011
Messages
133
Office Version
  1. 365
Platform
  1. Windows
Dear All,
I would like to use VBA to create named ranges from an extensive table, but i dont know how to write this specific code.
The names to be used as the name for the named ranges are in row6, starting at column B onwards to the right.
The data that should be in the range is varying in lenght. It is positioned in the column below the proposed name.
Some ranges will only be 3 cells long/high, others can be as long as 12 or 15 cells

How would a VBA code look like that scans this data and creates the named ranges accordingly? The named ranges should be available in the whole workbook.
Is it possible to also use such code to apply changes to the ranges? So if a column gets longer or shorter as the amount of cells in a range changes, the code can be run to reflect that in the named ranges?

Thansk for looking at this

Best Regards
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Perhaps.
Code:
Sub MakeNames()
Dim rngCol As Range
Dim rng As Range

     Set rngCol = Range("B6")

     Do Until rngCol.Value = ""

         Set rng = Range(rngCol.Offset(1), Cells(Rows.Count, rngCol.Column).End(xlUp))
         rng.Name = rngCol.Value
 
         Set rngCol = rngCol.Offset(,1)
     Loop

End Sub
 
Upvote 0
Hi,
thanks for the suggestion
it now gives an error at:
rng.Name = rngCol.Value
 
Upvote 0
Any particular error?

Are all the values in row 6 valid names?
 
Upvote 0
Hi Norie,
there was an invalid charachter in one of the names, so that solved it.
further it now works very quick and smooth.
It also allows to update the ranges once the data changes. i was afraid it woudl block there, but it gave no problems.
Thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,215,422
Messages
6,124,808
Members
449,191
Latest member
rscraig11

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