Unanswered: Problem setting range object
I have a problem setting a range object to be returned from one of my functions. I use a set keyword to set the return value (which is accepted), but I get the error:
"Object variable or With block variable not set (Error 91)"
when I run the code.
I call the function with:
and the function itself is:
rTarget = FindVal(wstTarget.Name, sDate, sTOB)
Does anyone have any idea what is wrong? When I debug the code, I get the error on the End Function line, but it steps through all the rest of the code in the fuction OK.
Function FindVal(sSheet As String, sDate As String, sTOB As String) As Range
' Return cell range for target value
Dim iRow As Integer
Dim iCol As Integer
iRow = 2
iCol = 2
Dim wstSheet As Worksheet
Set wstSheet = ThisWorkbook.Worksheets(sSheet)
Do Until wstSheet.Cells(iRow, 1).Value = sTOB
iRow = iRow + 1
Do Until wstSheet.Cells(1, iCol).Value = sDate
iCol = iCol + 1
If wstSheet.Cells(1, iCol).Value = "" Then
Set FindVal = wstSheet.Cells(iRow, iCol)
Make something idiot proof and someone will make a better idiot...