Jonmo1
MrExcel MVP
- Joined
- Oct 12, 2006
- Messages
- 44,061
Have you ever wanted to do a backwards Vlookup, to find a value in say column G, and return the corrosponding value from column D ?
Well, the thought just occured to me today that I can make a UDF to do just that....
Almost exactly the same as Vlookup...But reading Right to Left. Plus, you don't have to put in the True/False argument....honestly, how often do you use "True" in a Vlookup??? I can probably work that in there, if anybody actually wants it.
so there are 3 arguments in the LEFTLOOKUP function..
exampe formula using LEFTLOOKUP
A1 is the value to find in column M
E:M is the Range to look in, (again, it looks for A1 in column M)
Returns the Value in the 9th column to the LEFT from M (in this case E)
looks for exact matches, and does not need to be sorted. But if there are dupes, it will return the TOP value.
I added a couple twists...
instead of returning #N/A when the match is not found, it actually returns "Not Found"
And returns "Blank" instead of 0 when the resulting cell is blank.
Hope this helps
-------
and for those that don't know what a UDF is and how to use it...It's called a User Defined Function. To install a UDF, right click the excel icon in top left corner next to file...view code
click insert - module
paste code in that white window.
Now you can use the function like any other formula in excel.
Well, the thought just occured to me today that I can make a UDF to do just that....
Code:
Public Function LeftLookup(Myval As Variant, Myrange As Range, Col As Long)
lookupcolumn = Myrange.Column + Myrange.Columns.Count - 1
lookupstartrow = Myrange.Row
lookupendrow = Myrange.Row + Myrange.Rows.Count - 1
resultcol = 0 - Col + 1
result = "Not Found"
For i = lookupstartrow To lookupendrow
If Cells(i, lookupcolumn).Value = Myval Then
result = Cells(i, lookupcolumn).Offset(0, resultcol)
GoTo valuefound
End If
Next i
valuefound:
If result = "" Then result = "Blank"
LeftLookup = result
End Function
Almost exactly the same as Vlookup...But reading Right to Left. Plus, you don't have to put in the True/False argument....honestly, how often do you use "True" in a Vlookup??? I can probably work that in there, if anybody actually wants it.
so there are 3 arguments in the LEFTLOOKUP function..
exampe formula using LEFTLOOKUP
Code:
=LEFTLOOKUP(A1,E:M,9)
A1 is the value to find in column M
E:M is the Range to look in, (again, it looks for A1 in column M)
Returns the Value in the 9th column to the LEFT from M (in this case E)
looks for exact matches, and does not need to be sorted. But if there are dupes, it will return the TOP value.
I added a couple twists...
instead of returning #N/A when the match is not found, it actually returns "Not Found"
And returns "Blank" instead of 0 when the resulting cell is blank.
Hope this helps
-------
and for those that don't know what a UDF is and how to use it...It's called a User Defined Function. To install a UDF, right click the excel icon in top left corner next to file...view code
click insert - module
paste code in that white window.
Now you can use the function like any other formula in excel.