Excel - Create Named Ranges from Column Headers
If you've ever wanted to create a series of named ranges from column headers, the following contains code that you'll need to add to the VB Editor in Excel (CTRL+F11):
Sub Create_Named_Ranges()
Dim wb As Workbook, ws As Worksheet
Dim lrow As Long, lcol As Long, i As Long
Dim myName As String, Start As String
Dim wsName As String
' set the row number where headings are held as a constant
' change this to the row number required if not row 1
Const Rowno = 1
' set the Offset as the number of rows below Rowno, where the
' data begins
Const Offset = 2
' set the starting column for the data, in this case 1
' change if the data does not start in column A
Const Colno = 1
TK = Rowno + Offset
' On Error GoTo CreateNames_Error
Set wb = ActiveWorkbook
Set ws = ActiveSheet
' count the number of columns used in the row designated to
' have the header names
lcol = ws.Cells(Rowno, 1).End(xlToRight).Column
frow = ws.Cells(TK, Colno).End(xlDown).Row
Start = Cells(Rowno, Colno).Address
'replace blanks in worksheet names with underscore for the purposes of adding range names
wsName = ws.Name
wsName = Replace(wsName, " ", "_")
wb.Names.Add Name:=wsName & "_lcol", RefersTo:="=COUNT($" & Rowno & ":$" & Rowno & ")"
'wb.Names.Add Name:=wsName & "_lrow", RefersToR1C1:="=COUNT(C" & Colno & ")"
wb.Names.Add Name:=wsName & "_lrow", RefersToR1C1:="=COUNT(" & "R" & TK & "C" & Colno & ":" & "R" & frow & "C" & Colno & ")"
wb.Names.Add Name:=wsName & "_myData", RefersTo:="=" & Start & ":INDEX($1:$65536," & frow & "," & wsName & "_lcol)"
'wb.Names.Add Name:=wsName & "_myData", RefersTo:="=" & Start & ":INDEX($1:$65536," & wsName & frow & "," & wsName & "_lcol)"
For i = Colno To lcol
' if a column header contains space or other invalid character etc, replace with underscore
myName = Replace(Cells(Rowno, i).Value, "/", "_")
myName = Replace(myName, " ", "_")
myName = Replace(myName, "-", "_")
myName = Replace(myName, "<", "LT")
myName = Replace(myName, ">", "GT")
myName = Replace(myName, "=", "EQ")
myName = Replace(myName, "#", "NUM")
myName = Replace(myName, "&", "_")
myName = Replace(myName, "(", "_")
myName = Replace(myName, ")", "_")
myName = Replace(myName, "?", "_")
myName = Replace(myName, "\", "_")
If myName = "" Then
' if column header is blank, warn the user and stop the macro at that point
' names will only be created for those cells with text in them.
MsgBox "Missing Name in column " & i & vbCrLf _
& "Please Enter a Name and run macro again"
Exit Sub
End If
wb.Names.Add Name:=wsName & "_" & myName, RefersToR1C1:= _
"=R" & Rowno + Offset & "C" & i & ":INDEX(C" & i & "," & frow & ")"
nexti:
Next i
On Error GoTo 0
MsgBox "All dynamic Named ranges have been created"
Exit Sub
CreateNames_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CreateNames"
End Sub
This code will Replace characters that can't be included in Named Ranges. If you run into an error creating the Named Ranges, confirm that you don't have any additional characters not handled in the above Replace functions.
When complete, you can use the Name Manager to confirm that all named ranges were completed as expected and review the names that were assigned.
To access named ranges cell by cell (in a function), use the OFFSET function.


Comments
Post new comment