schielrn
Well-known Member
- Joined
- Apr 4, 2007
- Messages
- 6,941
This is what I came up with for positives or negatives, but it could definitely be cleaned upo:
It uses the vlookup function and index/match, but thats ok it made the code a little shorter in my opinion. You can still use a bigger table than is expected like with the original formula. So you can input:
=vlook(B2,E:H,4,0)
=vlook(B2,H:E,-4,0)
=vlook(B2,H:A,-4,0)
Code:
Function vlook(lookupValue As Variant, lookupRange As Range, columnNumber As Integer, Optional trueFalse As Boolean)
On Error GoTo handler
If columnNumber > 0 Then
vlook = Application.WorksheetFunction.VLookup(lookupValue, lookupRange, columnNumber, trueFalse)
ElseIf columnNumber < 0 Then
vlook = Application.WorksheetFunction.Index(Range(Mid(lookupRange.Address, InStr(1, lookupRange.Address, ":") + 1, 10) & ":" & Mid(lookupRange.Address, InStr(1, lookupRange.Address, ":") + 1, 10)).Offset(, columnNumber + 1), Application.WorksheetFunction.Match(lookupValue, Range(Mid(lookupRange.Address, InStr(1, lookupRange.Address, ":") + 1, 10) & ":" & Mid(lookupRange.Address, InStr(1, lookupRange.Address, ":") + 1, 10)), 0))
Else
Exit Function
End If
Exit Function
handler:
vlook = Evaluate("=na()")
End Function
=vlook(B2,E:H,4,0)
=vlook(B2,H:E,-4,0)
=vlook(B2,H:A,-4,0)