OK, so just see if I've got this straight.
You have a route code with a mileage.
On your data table you want to find the cat code that corresponds to the lowest value of End Miles - Start Miles, where the range of miles between start and end incorporates the mileage from table 1.
Is that correct?
If so my original code just needs a little tweak.
Function FindLowest(MileCode, Mileage, TableRange As Range) As Integer
Const TableSearchCol = 1
Dim FindMile As Range
Dim Lowest, FirstAddress
Dim StartMile, EndMile
Dim Cat
Dim CompareValue
Lowest = -1
Set FindMile = TableRange.Columns(TableSearchCol).Find(MileCode)
If Not FindMile Is Nothing Then
FirstAddress = FindMile.Address
While Not FindMile Is Nothing
StartMile = FindMile.Offset(0, 1).Value
EndMile = FindMile.Offset(0, 2).Value
CompareValue = Abs(EndMile - StartMile)
If StartMile=<Mileage And EndMile=> Mileage Then
If Lowest = -1 Then
Lowest = CompareValue
Cat = FindMile.Offset(0, 3).Value
Else
If CompareValue < Lowest Then
Lowest = CompareValue
Cat = FindMile.Offset(0, 3).Value
End If
End If
End If
Set FindMile = TableRange.Columns(TableSearchCol).FindNext(FindMile)
If FindMile.Address = FirstAddress Then
Set FindMile = Nothing
End If
Wend
FindLowest = Cat
Else
FindLowest = 0
End If
End Function
So you'll now have to pass the function 3 parameters - to get the lowest cat code for ABC call:
BestCat=FindLowest("ABC",123.456,sheets(2).range("A2:D6"))
Bookmarks