Saqib Husain
asked on
Checking abort condition in VBA UDF
I am using this statement in a UDF which works fine if the value of st is found in the range A:A.
sabbr = Sheet2.Range("A:A").Find(s t, , , xlWhole).Offset(0, 1)
But if the data is not found the code execution is aborted. Can someone show me how to check the presence of st in the range before executing the above statement so that the execution is not aborted.
sabbr = Sheet2.Range("A:A").Find(s
But if the data is not found the code execution is aborted. Can someone show me how to check the presence of st in the range before executing the above statement so that the execution is not aborted.
typo :)
Dim sabbr As Range
Set sabbr = Sheet2.Range("A:A").Find(st, , , xlWhole)
If Not sabbr Is Nothing Then
MsgBox sabbr.Offset(0, 1).Value
Else
MsgBox "didn''t find it"
End If
ASKER
Dave, the assignment is in a udf and not a sub. As soon as the statement is executed the function is aborted. The next statement is not executed.
Saqib
Saqib
Saqib
If I run my sample like this then the UDF either gives me the Offset value or a "no find" message
It won't abort
Cheers
Dave
If I run my sample like this then the UDF either gives me the Offset value or a "no find" message
It won't abort
Cheers
Dave
Function Test() As String
Dim sabbr As Range
Dim st
st = "test"
Set sabbr = Sheets(2).Range("A:A").Find(st, , , xlWhole)
If Not sabbr Is Nothing Then
Test = sabbr.Offset(0, 1).Value
Else
Test = "didn''t find it"
End If
End Function
Sub TestMe()
MsgBox Test
End Sub
ASKER
Saaib,
What do you want to happen once
HR: MARULA DITCH MR & METER FLUME,OUTLET
is not found?
Cheers
Dave
What do you want to happen once
HR: MARULA DITCH MR & METER FLUME,OUTLET
is not found?
Cheers
Dave
FWIW, you'd be better off passing all ranges as arguments to the function. They should not really be hardcoded in the UDF itself.
ASKER
Dave,
If it does not find it in the range I want it to continue with the routine as though the statement was not there.
In fact if the match is found I would like it to exit the routine after assigning the value of sabbr to the function.
Rory,
Yes rory, I agree but for my present assignment there is hardly going to be changes to the ranges.
Saqib
If it does not find it in the range I want it to continue with the routine as though the statement was not there.
In fact if the match is found I would like it to exit the routine after assigning the value of sabbr to the function.
Rory,
Yes rory, I agree but for my present assignment there is hardly going to be changes to the ranges.
Saqib
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
wow, typo 2
Set sabbr1 = Sheet2.Range("A:A").Find(st, , , xlWhole)
If Not sabbr1 Is Nothing Then sabbr = sabbr1.Offset(0, 1)
Saqib,
Doesn't really seem like much of a reason to me, especially as assignments have a tendency to mutate. ;)
Rory
Doesn't really seem like much of a reason to me, especially as assignments have a tendency to mutate. ;)
Rory
ASKER
Dave,
got the point, thanks.
Rory,
I wonder if you misunderstood me. By assignment I meant "the task I am working at" and not "the assignment statement in the question"
Saqib
got the point, thanks.
Rory,
I wonder if you misunderstood me. By assignment I meant "the task I am working at" and not "the assignment statement in the question"
Saqib
Cheers Saqib
>By assignment I meant "the task I am working at
That's what Rory meant. Inevitable scope creep :)
That's what Rory meant. Inevitable scope creep :)
ASKER
Well in that case I would just keep it as it is. There is a lot of work to be done to streamline it. I have been working since a year on it and would need it only for a few months more.
After that... who cares...
;-)
After that... who cares...
;-)
ie
Dave
Open in new window