0

I am trying to create named ranges of cells named after the top left cell in each range. This shows what I mean: enter image description here

Here, I want to have 4 named ranges (Names A, Names B, Names C, Names D) so I can reference them. Is there a way to have excel look for any cell in Column B that starts with "Names" and have it cut off the range when it hits the next cell that starts with "Names"? I apologize if this is not possible, I am still new to excel and not sure if this is doable. Any help is appreciated!

1 Answer 1

3

This is a quick thrown together script to provide the idea of how to approach this. By no means is it pretty, but does show the steps. This can be a lot more complexly written, ie to work with all cells, not start after the first instance of names (See Range("B3") for start, and Range("B4") for starting the looping of cells)

Sub DefineRanges()
    Dim rngStart As Range
    Set rngStart = Range("B3")
    Dim NamedRangeCount As Integer
    NamedRangeCount = 0
    Dim NameRangeName As String
    Dim LastRow As Integer
    For Each cell In Range("B4:B18")
        If LCase(Left(cell.Value, 5)) = "names" Then
            NameRangeName = "Name_" & NamedRangeCount
            ActiveWorkbook.Names.Add Name:=NameRangeName, RefersToLocal:=Range(rngStart.Address & ":D" & cell.Row - 1)
            Set rngStart = Range("B" & cell.Row)
            NamedRangeCount = NamedRangeCount + 1
        End If
        LastRow = cell.Row
    Next
    NameRangeName = "Name_" & NamedRangeCount
    ActiveWorkbook.Names.Add Name:=NameRangeName, RefersToLocal:=Range(rngStart.Address & ":D" & LastRow)
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly as I wanted it to! Thank you for making it simple, I can easily follow what you did and understand it better now. I appreciate your help very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.