Question on Vlookupnth

crunchtime9999

Board Regular
Joined
Feb 26, 2007
Messages
214
Hello,
I used the following function code from the board to return multiple values for a vlookup.
I looked on the forum for how to use code tags but could not find so please forgive me for not being able to do this. (Please let me know how to do the code tags)

code:
Public Function VlookupNth(MyVal As Variant, MyRange As Range, Optional ColRef As Double, _
Optional Nth As Double = 1)
'Similar to Vlookup, but returns the Nth value found from the top of myrange.
'Not necessarily the First.
'The FALSE argument usually found in Vlookup is assumed FALSE in this function. Data does NOT need
'to be sorted, and it searches for EXACT match.
'if ColRef is omitted, uses the number of columns in myrange
'if Nth is omitted, returns the first value found

Dim Count, i As Long
Count = 0
If ColRef = 0 Then ColRef = MyRange.Columns.Count
For i = 1 To MyRange.Rows.Count
If MyRange.Cells(i, 1).Value = MyVal Then
Count = Count + 1
If Count = Nth Then
VlookupNth = MyRange.Cells(i, ColRef).Value
Exit Function
End If
End If
Next i
VlookupNth = "Not Found"
End Function
code:\

I tried the formula and the results did not come out as I expected. Can someone here help me to understand what I am doing wrong.
aging extra info test.xls
ABCD
1OrderNumberProductcodeWarehouse
2501567LBV6765North15
3501619HRY654North23
4501619LO0987East65
5501619D4532West12
6501619FRE456East25
7501619WES222West99
8501622POI890Central77
9
10Test
11501567LBV6765North15
12501619LO0987East65
13501619D4532West12
14501619FRE456East25
15501619WES222West99
16501619NotFoundNotFound
17501622NotFoundNotFound
Sheet1
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
This part of the formula
Row(A1) is determining which occurance to return, the 1st 2nd 3rd etc...

in B11, Row(A1) = 1
When you fill that formula down, A1 incriments to A2
So in B12, it is now Row(A2) so it returns the 2nd occurance, not the first.

So at each Change in lookup value A11:A17, start it over at 1.

Try using this formula without the complicated Row() and COLUMN() Functions.
It's a good idea to understand what the formula does first, then try to make it automated and easier..

in B11
=vlookupnth($A11,$A$1:$C$8,2,1)
Copy Paste that formula Down and Right through your whole range B11:C17)
In column B, change all the 2's to 3s..

In All formulas, Change the 1 to whatever is the Nth Occurance you want returned...
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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