rename top row to allow named ranges

Mashtonsmith

Board Regular
Joined
Oct 1, 2004
Messages
164
Hi

Ideally I need something that will rename any cells in the top row into a format conducive for named ranges..

Eg - "Your title here" would become Your_title_here

I then want the contents of the each column to be a named range.

Column A is titled "Member Name" and has names from A2:A422 - within that are random blank cells.

The macro would rename A1 as Member_Name and then create a named range from A1:A422 called "Member_Name"

It would then work its way from Left to right until all column have had the top row names changed and all will have named ranges.

Thanks!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about
Code:
Sub NameRngs()
   Dim i As Long
   Range("1:1").Replace " ", "_", xlPart, , , , False, False
   For i = 1 To Cells(1, Columns.count).End(xlToLeft).Column
      Range(Cells(1, i), Cells(Rows.count, i).End(xlUp)).Name = Cells(1, i).Value
   Next i
End Sub
 
Upvote 0
Seems to work ok - until it doesn't like one of the column names. And then I cant find which one it fails on!
 
Last edited:
Upvote 0
When it fails click "Debug" & hover the mouse over
Code:
Cells(1, i).Value
What does it say?
 
Upvote 0

Forum statistics

Threads
1,215,206
Messages
6,123,636
Members
449,109
Latest member
Sebas8956

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