Created
September 23, 2017 09:09
-
-
Save jbruchanov/61a5b79bedf431d3776c1eb4be7c1c0a to your computer and use it in GitHub Desktop.
XVLookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function XVLookup(value As Range, dataSet As Range, returnCol As Integer, Optional indexValue As Integer = 0) As Variant | |
Dim val As String | |
Dim found As Integer | |
val = value.Cells | |
For counter = 1 To dataSet.Rows.Count | |
Dim cellval As String | |
cellval = dataSet.Cells(counter, 1) | |
If cellval = val Then | |
found = found + 1 | |
If indexValue = found Then | |
cellval = dataSet.Cells(counter, returnCol) | |
XVLookup = cellval | |
Exit Function | |
End If | |
End If | |
XVLookup = "#NA" | |
Next counter | |
End Function | |
Function XVLookupAll(value As Range, dataSet As Range, returnCol As Integer) As Variant | |
Dim val As String | |
Dim found As Integer | |
val = value.Cells | |
Dim result As String | |
For counter = 1 To dataSet.Rows.Count | |
'Row = dataSet.Row | |
Dim cellval As String | |
cellval = dataSet.Cells(counter, 1) | |
If cellval = val Then | |
cellval = dataSet.Cells(counter, returnCol) | |
If Len(result) > 0 Then | |
result = result & "," | |
End If | |
result = result & cellval | |
End If | |
Next counter | |
XVLookupAll = result | |
End Function | |
Function XVLookupCount(value As Range, dataSet As Range) As Variant | |
Dim val As String | |
Dim found As Integer | |
val = value.Cells | |
For counter = 1 To dataSet.Rows.Count | |
'Row = dataSet.Row | |
Dim cellval As String | |
cellval = dataSet.Cells(counter, 1) | |
If cellval = val Then | |
found = found + 1 | |
End If | |
Next counter | |
XVLookupCount = found | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment