Dynamic named ranges

Dorward

New Member
Joined
Mar 19, 2012
Messages
6
I have looked around and there are plenty of threads about this topic, but nothing really for what I'm looking for.

Essentially, I have a list of tasks. Each week, I am pasting a new set of tasks into the same sheet. However, many of the values are the same (task headers). These headers are not specified in any real way other than being bolded.
What I need though is from Task X to Task Y-1 to be named Range X. Task Y to Task Z-1 is named range Y. I can use static cell references because the cell reference for those tasks changes.

Any idea how to help me out with this?
Cheers.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
But in determining where ranges should start/stop, I am looking for "PMO", not "*PMO*", right?
 
Upvote 0
If my previous assumption is correct, then this code does what you are asking for:
Code:
Sub myRangeName()
 
    Dim myLookupRange As Range
    Dim myFirstCell As Long
    Dim myLastCell As Long
    Dim myLastRow As Long
    Dim cell As Range
    Dim myRangeName As String
    Dim myNextRangeName As String
    Dim myNamedRange As String
    Dim myCount As Long
    Dim i As Long
        
'   Specify range lookup list is found in
    Set myLookupRange = Range("M1:M4")
    myCount = myLookupRange.Count
    
'   Find last row in column A
    myLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Get start cell
    myRangeName = myLookupRange(1, 1).Text
    Range("A1:A" & myLastRow).Find(What:=myRangeName, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    myFirstCell = ActiveCell.Row
'   Loop through lookup range finding addresses
    For i = 1 To myCount
        myNextRangeName = myLookupRange(i + 1, 1).Text
'   Look for next range name
        If i = myCount Then
            myLastCell = myLastRow
        Else
            Range("A" & myFirstCell & ":A" & myLastRow).Find(What:=myNextRangeName, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False).Activate
            myLastCell = ActiveCell.Row
        End If
'   Name range (may need to adjust sheet name)
        myNamedRange = "=Sheet1!$A$" & myFirstCell & ":$A$" & myLastCell - 1
        ActiveWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRange
'   Reset variables
        myRangeName = myNextRangeName
        myFirstCell = myLastCell
    Next i
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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