Thought I would share this VBA that creates named ranges from the value of the cell. I hunted for this solution (about 100 different google searchs) and finally just figured it out.
What is cool about this script is that no matter where the "named range" is in the worksheet, can be created as it is not based on a absolute range reference. This works really well if you have the same named ranged that are used in other calculations, and has saved my sanity!
If anyone has ideas on how to make it more efficient that would be great!
Sub CreateNamedRanges()
Dim xlWB As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim xRng As Range
' *** Sets named ranges based off of cell.value
' *** Dynamic as named ranges are not absolute
Set xlWB = ThisWorkbook
Set ws = xlWB.Worksheets("Sheet1")
Set rng = ws.Range("A1", ws.Range("A1").End(xlToRight))
For Each cell In rng
If cell <> "" Then
' <== drop the range down one cell as offset() will not work
firstaddress = cell.Rows(2).Address
lastaddress = cell.Rows(65535).End(xlUp).Address
ws.Range(firstaddress, lastaddress).Name = cell.Value
End If
Next cell
End Sub
Bookmarks