Thasnks for your help. I still am not getting my function to work as I want. Basically I am trying to search a 4 column array, matching in the first 3 columns and returning the resulting 4th col;umn. Here is my function:
Function PRMRate(RateTable As Range, ByVal Customer As Variant, Pickup As Variant, Drop As Variant)
Dim a As Integer, TestCust As Variant, TestPickup As Variant, TestDrop As Variant
'Get size of dataset
lastrow = RateTable.Cells(1, 1).Value
'Initialize return argument
PRMRate = 0
'Loop thru data for complete match of 3 fields
For a = 1 To lastrow
TestCust = RateTable.Cells(a, 1).Value
' Is This the Right Customer?
If (Customer = TestCust) Then
' Is This the Right Pickup point for This Customer?
TestPickup = RateTable.Cells(a, 2).Value
If (Pickup = TestPickup) Then
' Is This the Right Drop Point for This Combination?
TestDrop = RateTable.Cells(a, 3).Value
If (Drop = TestDrop) Then
' Yes - Set the Rate for This Combination
PRMRate = RateTable.Cells(a, 4).Value
End If
End If
End If
Next a
End Function
The range I am searching looks like this:
20
CustomerName PickupPoint DropPoint Rate
A C G 1
A C H 2
A C I 3
A D J 4
A D K 5
A D L 6
B E M 7
B E N 8
B E O 9
B F P 10
B F Q 11
B F R 12
It seems pretty simple, but then, too, I'm self-taught, so I'm pretty simple, too, and a novice in VBA. I start the search in the first column If I match in the first column, then check the second column of the same row for a match. If that is true, then I check the third column of the same row for a match. If that is true, then I return the value in the fourth column of the same row as the function value. Wahere am I wrong?
Thanks for any help anyone can give.
|