Derek Brown
Well-known Member
- Joined
- Dec 26, 2005
- Messages
- 2,390
Re: June/July 2008 Challenge of the Month
An update to my solution of 9th July, with a change of function name to ReverseVLOOKUP, this includes more argument validation and the option to check the entire content of the cell.
The first argument is the lookup value, the second is the table array, the third is the column to return and the new argument is a boolean (true or false) to indicate whether the entire cell is to match.
An update to my solution of 9th July, with a change of function name to ReverseVLOOKUP, this includes more argument validation and the option to check the entire content of the cell.
Code:
Function ReverseVLOOKUP(rngCell As Range, rngTable As Range, lngColumn As Long, _
Optional blEntireCellContent As Boolean) As String
Application.Volatile
Dim rng As Range
Dim blEntireCell As Boolean
If ((lngColumn > rngTable.Columns.Count) Or (lngColumn < 1) Or _
(rngCell.Columns.Count > 1) Or (rngCell.Rows.Count > 1)) Then
ReverseVLOOKUP = "#REF!"
Exit Function
End If
If IsMissing(blEntireCellContent) Then
blEntireCell = False
Else
blEntireCell = blEntireCellContent
End If
For Each rng In rngTable.Columns(1).Cells
If blEntireCell Then
If rngCell.Value = rng.Value Then
ReverseVLOOKUP = Cells(rng.Row, rng.Column + lngColumn - 1)
Exit For
End If
ElseIf InStr(1, rngCell.Value, rng.Value) > 0 Then
ReverseVLOOKUP = Cells(rng.Row, rng.Column + lngColumn - 1)
Exit For
End If
Next rng
Set rng = Nothing
End Function