Hi everyone, I'm trying to write a function that searches a range for a particular value and returns the value of a cell that is offset from the matching cell by the values specified in the other arguments. My code is:
Function LOOKUPDATA(LookupVal As Date, OffsetRow As Integer, OffsetCol As Integer) As Integer
Dim Cell As Range
For Each Cell In Worksheets("Data by month").Range("A1:A100")
If Cell.Value = LookupVal Then
LOOKUPDATA = Cell.Offset(OffsetRow, OffsetCol).Value
End If
Next Cell
End Function
This runs perfectly but always returns a value of zero, no matter what values I give the arguments. I've tried using a message box to see the value of each cell as it steps through the range, and even when the value in the message box is the same as that in the first argument it just keeps on going.
What am I doing wrong?
Function LOOKUPDATA(LookupVal As Date, OffsetRow As Integer, OffsetCol As Integer) As Integer
Dim Cell As Range
For Each Cell In Worksheets("Data by month").Range("A1:A100")
If Cell.Value = LookupVal Then
LOOKUPDATA = Cell.Offset(OffsetRow, OffsetCol).Value
End If
Next Cell
End Function
This runs perfectly but always returns a value of zero, no matter what values I give the arguments. I've tried using a message box to see the value of each cell as it steps through the range, and even when the value in the message box is the same as that in the first argument it just keeps on going.
What am I doing wrong?