VBA, Match, Worksheet_Change and named ranges

cornflakegirl

Well-known Member
Joined
Nov 4, 2004
Messages
2,023
Two related questions in one.

I have this line of VBA:
Code:
If Not IsNumeric(Application.Match(sAgg, Sheet6.Range(sRange), 0)) Then Range("Product").Value = "All"

sAgg and sRange are strings. sRange contains the name of a named range.

I originally had Application.WorksheetFunction.Match, but had to take WorksheetFunction out because the Match evaluates to error - is that the proper way to deal with that?

Also, this code is in a Worksheet_Change routine. My named range Range(sRange) is on a different worksheet. I originally tried to use just Range(sRange) but it errored until I defined which sheet it was on. Is there a way that I can use the named range without specifying the sheet? (I tried ActiveWorkbook.Range(sRange) but that didn't work either.)
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi.

Yes Application.Match is the right way to go.

If it is a worksheet specific defined name then you have to qualify it with the sheet name, index or codename.
 
Upvote 0
This might Help:-
Code:
Dim ws As Worksheet
Dim rng As Range
Set ws = ActiveWorkbook.Names("sRange").RefersToRange.Worksheet
MsgBox ws.Name
Set rng = ActiveWorkbook.Names("sRange").RefersToRange
MsgBox rng.Address
If Not IsNumeric(Application.Match(sAgg, rng, 0)) Then Range("Product").value = "All"
Mick
 
Upvote 0
@VoG - it's not a worksheet specific name. I'm using it in the actual worksheet that the code belongs to without a problem, and there aren't any other named ranges with the same name.

@Mick - thanks for that. Will go and look up what RefersToRange does.
 
Upvote 0
RefersToRange is just what I need.

For the benefit of anyone else - I found this thread where pgc explains the problem a bit further, and suggests the same solution.

Thanks both very much.
 
Upvote 0

Forum statistics

Threads
1,214,524
Messages
6,120,049
Members
448,940
Latest member
mdusw

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