VBA - INDEX,MATCH with named ranges (Confused as!!)

Scorpion Steve

Board Regular
Joined
Jun 5, 2011
Messages
220
Hi all!
Happy New years to start of with.
I'm trying to use two different named ranges nested within a string using the INDEX/MATCH function.

I've tried everything and still getting errors:
Run-time error '13':
Type Mismatch
for:
Code:
Application.WorksheetFunction.Index(SearchInput, rng1, Application.Match(SearchInput, rng2, 0, 1)) = ContactNo.Value


&
Run-time error '1004':
Invalid number of arguments.
Code:
Application.WorksheetFunction.Index(SearchInput, Range("rng1"), Application.Match(SearchInput, Range("rng2"), 0, 1)) = ContactNo.Value

Code snippet:
Code:
Dim rng1 As Range
        Set rng1 = ActiveWorkbook.Names("PCIDRange").RefersToRange
           Dim ws As Worksheet
                Set ws = ActiveWorkbook.Names("PCIDRange").RefersToRange.Worksheet
    Dim rng2 As Range
        Set rng2 = ActiveWorkbook.Names("ContactNoRange").RefersToRange
           Dim ws2 As Worksheet
                Set ws2 = ActiveWorkbook.Names("ContactNoRange").RefersToRange.Worksheet


[COLOR=#0000ff]Application.WorksheetFunction.Index(SearchInput, rng1, Application.Match(SearchInput, rng2, 0, 1)) = ContactNo.Value[/COLOR]
End If
Anyone got a clue?
Ta
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
That syntax should be the other way round I think
Rich (BB code):
ContactNo.Value = Application.WorksheetFunction.Index(SearchInput, rng1, Application.Match(SearchInput, rng2, 0, 1)) 
Give that a try. But then again, the second argument "rng1" should be a number so i dont know how you want to pull that off
 
Last edited:
Upvote 0
Hi Momentman.
I see what you're saying.
I'm trying to update the cell from the value within textbox ("SearchInput")

I think the way round that you suggest will update the textbox value instead of the cell value

But its a start on mucking around with where I place the ".value"

ta
 
Upvote 0
you are using vba to run a cell formula

the formula has to work in a cell in a worksheet

the formula does not expect a range object, it expects a string in the form of a range name ("PCIDRange") or range definition ("A5:G7" or "A1")

therefore, try putting the range names into the formula

Application.WorksheetFunction.Index(SearchInput, "PCIDRange", Application.Match(SearchInput, "ContactNoRange", 0, 1)) = ContactNo.Value

it also looks like you have the two sides of the equation reversed

maube it should be ContactNo.Value = ............

make the formula work in a cell first

then copy it into the vba code and make it a comment so that vba does not flag it as an error

then work from that as a guide
 
Last edited:
Upvote 0
Now I have Run-time '1004'
Method of 'Range' of object'_Global' failed
when:
Code:
Code:
Dim rng1 As Range
        Set rng1 = ActiveWorkbook.Names("PCIDRange").RefersToRange
           Dim ws As Worksheet
                Set ws = ActiveWorkbook.Names("PCIDRange").RefersToRange.Worksheet
    Dim rng2 As Range
        Set rng2 = ActiveWorkbook.Names("ContactNoRange").RefersToRange
           Dim ws2 As Worksheet
                Set ws2 = ActiveWorkbook.Names("ContactNoRange").RefersToRange.Worksheet


[COLOR=#0000ff]With Application.WorksheetFunction.Index(SearchInput, Range(rng1), Application.Match(SearchInput, Range(rng2), 0, 1))[/COLOR]
[COLOR=#0000ff].Value = ContactNo[/COLOR]
End With
End If
 
Upvote 0
Hi Justola.
Yeah, I started with that :/
Type mismatch

It works fine if I hard code the range but I'm needing it to be dynamic.
Well, having said that, I don't - I can be done hard coded - obviously.
I think I'm being too picky and thought there may be a simple solution.

I have over 200 dynamic ranges

Steve
 
Upvote 0
Hard Coded still doesn't work :?
Application.Index(Sheets("DATA SHEET").Range("G2:G250"), Application.Match(SearchInput, Sheets("DATA SHEET").Range("N2:N250"), 0), 1).value = ContactNo

This brings up:
Run-time '424'
Object required!
New idea:
Dim BED as Object
Dim Sleep as variant

lol
 
Upvote 0
this is just a starting point to solving your issue

small steps ....

can you do the formula in a cell somewhere on the worksheet so that the correct values are returned on the worksheet

put the parameter cells near that cell



then copy the formula and post it here

also post the parameter values
 
Last edited:
Upvote 0
Ppl - Figured it out!
My fault 1stly - I had the ranges the wrong way around so would have mucked up anyways.
I managed it from:
Code:
Application.Index(Sheets("DATA SHEET").Range("ContactNoRange"), Application.Match(SearchInput, (Sheets("DATA SHEET").Range("PCIDRange")), 0)) = ContactNo

Thanks to all. It actually helps ppl taking a stab in getting ideas working!

Steve
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

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