我需要将Range.Find()封装在一个函数中,因此我有以下代码(R1、C1等均为已定义的常量):
Function FindData(FindWhat As Variant) As Range
Dim TheR As Range
Set TheR = Range(Cells(R1, C1), Cells(R2, C2)).Find(FindWhat, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
FindData = TheR
End Function
我正在传递一个字符串,所以调用是例如:
Set FirstCell = Call FindData("MyValue")
VBA在该行FindData = TheR
出现错误,提示信息为Object variable or With block variable not set
。我尝试添加了以下if块:
If (TheR Is Nothing) Then
FindData = Nothing
Else
FindData = TheR
End If
但这并没有什么区别。我该如何返回Find()函数给我的值?
Dim TheR As Range
和Set FindData = TheR
,并直接使用Set FindData= Range(Cells(R1, C1), Cells(R2, C2)).Find(FindWhat, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
。 - user3598756