selecting named ranges that include particular text

keithmct

Active Member
Joined
Mar 9, 2007
Messages
256
Office Version
  1. 2021
Platform
  1. Windows
I want to select multiple price grids so as to increase them by a few percent if needed. They are all named ranges and all end in "prices". Can I do something in a macro like:
Range(*&"prices").Select ?
 

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)
Maybe something like this:
VBA Code:
Sub Test1()
Dim nm
Dim c As Range
For Each nm In ThisWorkbook.Names
    If nm.Name Like "*prices" Then  
'        Debug.Print nm.Name
        If c Is Nothing Then
           Set c = Range(nm.Name)
        Else
           Set c = Union(c, Range(nm.Name))
        End If
    End If
Next nm

'now, c is holding all those named ranges.
c.Select

End Sub
 
Upvote 0
Solution
tried putting the macro in my custom ribbon tab and now shows runtime error 91 Object variable or With block variable not set at line
c.select
 
Upvote 0
tried putting the macro in my custom ribbon tab and now shows runtime error 91 Object variable or With block variable not set at line
c.select
Where do you put the code? in Personal.xlsb?
 
Upvote 0
I tested it out in a normal active-x button on the relevant sheet which worked perfectly. Tried to customise ribbon to add macro but showing only macros, it wasn't available to add to Main Tab so I then copied it to a Personal.xlsb module, and that seemed to work except for the above error.
 
Upvote 0
Try using ActiveWorkbook.Names instead of ThisWorkbook.Names
VBA Code:
Sub Test2()
Dim nm
Dim c As Range
For Each nm In ActiveWorkbook.Names
    If nm.Name Like "*prices" Then
'        Debug.Print nm.Name
        If c Is Nothing Then
           Set c = Range(nm.Name)
        Else
           Set c = Union(c, Range(nm.Name))
        End If
    End If
Next nm

'now, c is holding all those named ranges.
If Not c Is Nothing Then
c.Select
Else
MsgBox "Can't find names that end with 'price'"
End If

End Sub

or try using your workbook name:
VBA Code:
For Each nm In Workbooks("something").Names
 
Upvote 0
you're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
further to the above, some of my named ranges with "prices" in their name are no longer used and I wanted to move them to a new worksheet that I can hide. Now when I use macro in post #7 above I get error:
Run-time error '1004':
Method 'Range' of object'_Worksheet' failed

and upon debug the line
Set c = Union(c, Range(nm.Name))
is highlighted in yellow. Any suggestions?

Code:
Private Sub selectprices_Click()
Dim nm
Dim c As Range
For Each nm In ActiveWorkbook.Names
    If nm.Name Like "*prices" Then
'        Debug.Print nm.Name
        If c Is Nothing Then
           Set c = Range(nm.Name)
        Else
           Set c = Union(c, Range(nm.Name))
        End If
    End If
Next nm

'now, c is holding all those named ranges.
If Not c Is Nothing Then
c.Select
Else
MsgBox "Can't find names that end with 'prices'"
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,627
Messages
6,173,425
Members
452,515
Latest member
Alicedonald9

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