OK, I tried again. You were right, the last one didn't work.
I've included a copy of the worksheet with the events stripped out; the events were causing problems for my version of excel.
Note that there are two ways to use the function. One is to enter an array of possible values in curly braces, the other is to refer to a range of values.
Copy of Planogram 2012-2013.xlsm
Public Function EventLookup(DateRange As Range, EventRange As Range, _
FindStrings As Variant) As Variant
Dim FutureDateRange As Range, FutureEventRange As Range, _
TodayPosition As Long, EventPosition As Long, Result As Variant, _
tmpVariant As Variant, tmpEventPosition As Long
'Set the ranges that refer to dates in the future. If none found, return
'a VALUE error
On Error GoTo BadDateRange
TodayPosition = Application.WorksheetFunction.Match(CLng(Now), DateRange, 1)
Set FutureDateRange = Range(DateRange.Cells(1, TodayPosition), _
DateRange.Cells(1, DateRange.Columns.Count))
Set FutureEventRange = Range(EventRange.Cells(1, TodayPosition), _
EventRange.Cells(1, EventRange.Columns.Count))
'Look for the event. if no event is found, return #N/A Error
EventPosition = 999999
tmpEventPosition = EventPosition
'FindStrings could be an array or a single value
If IsArray(FindStrings) Then
For Each tmpVariant In FindStrings
On Error Resume Next
tmpEventPosition = Application.WorksheetFunction.Match(tmpVariant, FutureEventRange, 0)
On Error GoTo 0
If tmpEventPosition < EventPosition Then
EventPosition = tmpEventPosition
End If
Next tmpVariant
Else
On Error Resume Next
tmpEventPosition = Application.WorksheetFunction.Match(FindStrings, FutureEventRange, 0)
On Error GoTo 0
If tmpEventPosition < EventPosition Then
EventPosition = tmpEventPosition
End If
End If
'If eventposition is larger than the number of columns, then return NA error
If EventPosition > 16384 Then
Result = CVErr(xlErrNA)
Else
Result = FutureDateRange.Cells(1, EventPosition).Value
End If
EventLookup = Result
Exit Function
BadDateRange:
EventLookup = CVErr(xlErrValue)
Exit Function
End Function
Bookmarks