I need some help debugging this VBA function. I know I'm probably glossing over a simple error, but I can't figure it out.
I have a VBA function that acts as a two-parameter VLOOKUP. Here it is:
Function TwoParameterVlookup(Data_Range As Range, Col As Integer, Parameter1 As _
Variant, Parameter2 As Variant) As Variant
'Declare Variables
Dim Cell
Dim Current_Row As Integer
Dim No_Of_Rows_in_Range As Integer
Dim No_of_Cols_in_Range As Integer
Dim Matching_Row As Integer
'set answer to N/A by default
TwoParameterVlookup = " "
Matching_Row = 0
Current_Row = 1
No_Of_Rows_in_Range = Data_Range.Rows.Count
No_of_Cols_in_Range = Data_Range.Columns.Count
'Check if Col is greater than number of columns in range
If (Col > No_of_Cols_in_Range) Then
TwoParameterVlookup = CVErr(xlErrRef)
End If
If (Col <= No_of_Cols_in_Range) Then
Do Until ((Current_Row > No_Of_Rows_in_Range) Or (Matching_Row <> 0))
If ((Data_Range.Cells(Current_Row, 1).Value = Parameter1) And (Data_Range.Cells(Current_Row, 2).Value = Parameter2)) Then
Matching_Row = Current_Row
End If
If Matching_Row <> 0 Then
TwoParameterVlookup = Data_Range.Cells(Matching_Row, Col)
End If
Current_Row = Current_Row + 1
Loop
End If
End Function
Originally, the function was closing the loop before the final row of the data range could be read. I fixed this, but the current problem still remains. I'll select the data range for my function to vlookup, but it will simply skip past the top row of my selected range.