0

I have a function that finds the current date in a range and returns the cell in which it's found. I'm trying to use that cell's position to calculate an offset within a subroutine. However, when I run the macro, I get objected required as an error. The function and the subroutine are as shown:

Function findCurrentDate() As Range
    Dim needle As Date
    Dim rng As Range
    needle = CLng(Date)
    Dim haystack As Range
    Dim search As Range
    Set haystack = Range("B3:B86")
    Set search = haystack.Find(needle, After:=haystack(1), _
                           LookIn:=xlValues, Lookat:=xlWhole, _
                           SearchOrder:=xlNext, MatchByte:=True)
End Function

Sub showFutureWeeklyHours()
    Dim wkday_row_offset As Integer
    Dim search As Range
    Set search = findCurrentDate()
    Set wkday_row_offset = search.Row
...
End Sub

This isn't the complete subroutine, but it's enough to reproduce the error. The list of dates are stored in cells B3:B86

1 Answer 1

2

This wkday_row_offset is a number, not an object, so do not use Set.

"This method returns Nothing if no match is found. The Find method does not affect the selection or the active cell."

http://msdn.microsoft.com/en-us/library/ff839746.aspx

You will need to check that something is returned before looking for the row property. Nothing does not have a row. A row is a number, not an object.

Function findCurrentDate() As Range
    Dim needle As Date
    Dim rng As Range
    needle = CLng(Date)
    Dim haystack As Range
    Dim search As Range
    Set haystack = Range("B3:B86")
    Set search = haystack.Find(needle, After:=haystack(1), _
                           LookIn:=xlValues, Lookat:=xlWhole, _
                           SearchOrder:=xlNext, MatchByte:=True)
    Set findCurrentDate = search
End Function

Sub showFutureWeeklyHours()
    Dim wkday_row_offset As Integer
    Dim search As Range
    Set search = findCurrentDate()

    wkday_row_offset = search.row
''...
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

If I remove the Set keyword from the wkday_row_offset line, I get the following error: Object variable or with block variable not set Any other ideas?
Based on your update, how would I check if something is returned? Since I tested this function several minutes ago, it does return a value because the search string 6/1/2012 does exist in the worksheet. Therefore, shouldn't the function (in this case) be returning a value?
You need to set the function to search, set findCurrentDate = search. You said this is a fragment and asked about one error.
Could you post a code snippet detailing what you're talking about? Changing this line: Set search = findCurrentDate() to your suggestion of: set findCurrentDate = search throws an Invalid use of property error I thought maybe you accidentally reversed it, so I tried this: Set search = findCurrentDate but that returns the same Object variable or with block variable not set error. Yes, this is a fragment, but it was enough to reproduce the first error, which is what I'm trying to fix.
The above is your code with the suggested changes and tested as working. You should test that search is not nothing as per my link before getting the row.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.