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.
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 | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | OrderNumber | Productcode | Warehouse | |||
2 | 501567 | LBV6765 | North15 | |||
3 | 501619 | HRY654 | North23 | |||
4 | 501619 | LO0987 | East65 | |||
5 | 501619 | D4532 | West12 | |||
6 | 501619 | FRE456 | East25 | |||
7 | 501619 | WES222 | West99 | |||
8 | 501622 | POI890 | Central77 | |||
9 | ||||||
10 | Test | |||||
11 | 501567 | LBV6765 | North15 | |||
12 | 501619 | LO0987 | East65 | |||
13 | 501619 | D4532 | West12 | |||
14 | 501619 | FRE456 | East25 | |||
15 | 501619 | WES222 | West99 | |||
16 | 501619 | NotFound | NotFound | |||
17 | 501622 | NotFound | NotFound | |||
Sheet1 |