How to Improve my "Vlookup" on Steroids function.....

M

MikeZz

I'd like to know if there is a way to speed up the following function because
I use it ALOT on larger source ranges.

This function is similar to Vlookup except:
Instead of passing the value in column 1 and a column number...
You pass the String representing the title of up to 3 columns-
DataCol = Column with data you want to retrieve.
Col1 = Column to filter by
Col2 = a second column to filter by
If there are multiple findes, it flags the answer

Here's an example.....

"TextA" "TextB" "TextC"
1 3 Z
1 4 X
1 4 Y
2 4 X
3 4 L

X2Find(range, "TextC", "TextA", 1, "TextB", 3) = "Z"
X2Find(range, "TextB", "TextA", 1, "TextC", "X") = 4
X2Find(range, "TextC", "TextA", 1, "TextB", 4) = "!! 2 Matches !!"


Function X2Find(rngTemp As Range, DataCol As Variant, Col1Title As Variant,
Col1Val As Variant, Col2Title As Variant, Col2Val As Variant)
'THIS FUNCTION is like VLookup except you:
' Provide 2 column Titles and search values...
'
'Range = with Titles
'DataCol = column you want the data from
'Col1Val = First Column Test
'Col1Title = First Column Title
'Col2Val = Second Column Test
'Col2Title = Second Column Title


rRows = rngTemp.Rows.Count
rCols = rngTemp.Columns.Count
foundCol1 = False
foundCol2 = False
foundall = False

For c = 1 To rCols
If rngTemp(1, c) = DataCol Then
DataCol = c
foundData = True
foundall = foundall + 1
End If
Next c
For c = 1 To rCols
If rngTemp(1, c) = Col1Title Then
Col1Title = c
foundCol1 = True
foundall = foundall + 1
End If
Next c
For c = 1 To rCols
If rngTemp(1, c) = Col2Title Then
Col2Title = c
foundCol2 = True
foundall = foundall + 1
End If
Next c

If foundall <> 3 Then
X2Find = "Missing Info"
Exit Function
End If

foundrows = False
For r = 1 To rRows
testval1 = rngTemp(r, Col1Title)
testval2 = rngTemp(r, Col2Title)
If testval1 = Col1Val And testval2 = Col2Val Then
foundrows = foundrows + 1
lastmatchingrow = r
End If
Next r

Select Case foundrows
Case False
X2Find = "No Match"
Case 1
X2Find = rngTemp(lastmatchingrow, DataCol)

Case Is > 1
X2Find = "!! " & foundrows & " Matches !!"

End Select

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top