vba to create named range columns

MikeL

Active Member
Joined
Mar 17, 2002
Messages
488
Office Version
  1. 365
Platform
  1. Windows
Hi I want to use VBA to create named range columns for all the US Presidents starting in Col E, Row 5. The names will all be listed across row 4. The row length of the data below is variable ie 100 to 200 rows so maybe use last row?

Thanks in advance,
Mike
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about
Code:
Sub MikeL()
    Dim Cl As Range
    For Each Cl In Range("E4", Cells(4, Columns.Count).End(xlToLeft))
        Range(Cl, Cl.End(xlDown)).CreateNames True
    Next Cl
End Sub
 
Upvote 0
Thank you. If several column headers have numbers (ie 0,1,2,3) etc it seems like there is not a named range created. Checking if this can be addressed in the macro?
 
Upvote 0
What values do you have in row 4?
 
Upvote 0
Row 4 has mostly strings of names (used Presidents as example) but also has numeric values. And xltoLeft looks at Col D to start then pretty much uses that range as it loops correct? Reason I ask is Col D may have some null values as compared to col A.
 
Upvote 0
What are the values when it doesn't create a named range?
 
Upvote 0
How about
Code:
Sub MikeL()
    Dim Cl As Range
    For Each Cl In Range("E4", Cells(4, Columns.Count).End(xlToLeft))
        If IsNumeric(Cl) Then
            Range(Cl, Cl.End(xlDown)).Name = "_" & Cl
        Else
            Range(Cl, Cl.End(xlDown)).CreateNames True
        End If
    Next Cl
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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