set range based on activecell and what range it is, but name must contain certain word

wbstadeli

Board Regular
Joined
Mar 11, 2016
Messages
153
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a worksheet with a lot of named ranges in it. My main named ranges are named "Item_Range_(Variable)" with the variable being a number so that the named ranges are unique. What i would like to do is in my macro set range to equal whichever range that the activecell is in and contains "Item_Range" in the name. How would this be coded? Thanks!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hello,

Does this work as expected?

VBA Code:
Sub WHOLE_RANGES()
    Range("Z1").ListNames
    For MY_ROWS = 1 To Range("Z" & Rows.Count).End(xlUp).Row
        If Not Intersect(ActiveCell, Range(Range("Z" & MY_ROWS).Value)) Is Nothing Then
            Range(Range("Z" & MY_ROWS).Value).Select ' YOU CAN DELETE THIS LINE
            MY_RANGE = Range("Z" & MY_ROWS).Value
            GoTo MY_END
        End If
    Next MY_ROWS
MY_END:
Columns("Z:AA").ClearContents
End Sub

I have assumed columns Z and AA are available. Is not, change all Z to a column that is available and change Z:AA to your available column and the one to the right of it.
 
Upvote 0
Interesting approach! Thank you for your response, i was able to get what i need with this:
VBA Code:
     Private Sub Worksheet_Change(ByVal target As Range)
    '.....
    Dim namItem As Name
    For Each namItem In ThisWorkbook.Names
        'match a certain partial range name, this example: Quoted_Price_Range_'
        If (Left(namItem.Name, 19) = "Quoted_Price_Range_")  Then
            If (Not (Intersect(target, namItem.RefersToRange) Is Nothing)) Then
                'do code'
            End If
        End If
    Next
    '......'
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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