I tried to search a value from another sheet rather using the .FIND function
Private Function Search(rng As Range, FindString As String) As Range
With rng
Set Search = .Find(what:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
The funny thing is that if I enter
Search(Range("'DataSheet'!A1:Z1000"),"STARTING")
It works. When I try
SearchString = "'" & Selected_sheet &"'!A1:Z1000"
It does not work Selected_sheet is just a name of a worksheet (In this case, it is "DataSheet"). Using
Search(Range(SearchString ),"STARTING")
Error: RUN-TIME ERROR '91'
Object variable or With Block Varibale Not Set
What could be the actual problem?
Your method is not incorrect but I recommend not passing your range like that.
Use this
or
BTW there is nothing wrong with
Range(SearchString)
in your code. It should work. How are you callingSearch
? Hope like this?Reason: The function returns a
Range
so you cannot just call it withoutSet Ret = ...
WhereRet
is again declared as aRange
.EDIT
You can also get that error if your
Search
function doesn't return a range and you try using thatRet
. See this example.So how do we tackle this
Try this