0

範囲内の現在の日付を検索し、見つかったセルを返す関数があります。そのセルの位置を使用して、サブルーチン内のオフセットを計算しようとしています。ただし、マクロを実行するobjected requiredと、エラーが発生します。関数とサブルーチンは次のとおりです。

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

これは完全なサブルーチンではありませんが、エラーを再現するには十分です。日付のリストはセルに保存されますB3:B86

4

1 に答える 1

2

これwkday_row_offsetはオブジェクトではなく数値なので、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

行プロパティを探す前に、何かが返されることを確認する必要があります。行がないものはありません。行は数値であり、オブジェクトではありません。

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
于 2012-06-01T18:47:15.347 に答える