Solved

Checking abort condition in VBA UDF

Posted on 2011-02-10
15
273 Views
Last Modified: 2012-06-27
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(st, , , 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.
0
Comment
Question by:Saqib Husain, Syed
  • 8
  • 5
  • 2
15 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34868352
Use a range object then test for nothing

ie

Dave
Sub d()
    Dim sabbr As Range
    Set sabbr = Sheet2.Range("A:A").Find(st, , , xlWhole).Offset(0, 1)
    If sabbr Is Nothing Then
        MsgBox sabbr.Value
    Else
        MsgBox "didn''t find it"
    End If
End Sub

Open in new window

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34868356
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

Open in new window

0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 34868483
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
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34868502
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
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

Open in new window

0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 34868560
Dave, please take a look at this file UDF-abort.xls

Cell F21 contains refereence to this UDF

Saqib
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34868769
Saaib,

What do you want to happen once
HR: MARULA DITCH MR & METER FLUME,OUTLET
is not found?

Cheers

Dave
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34868858
FWIW, you'd be better off passing all ranges as arguments to the function. They should not really be hardcoded in the UDF itself.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 34868881
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
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 34868902
Saqib,

You could use these two lines then.

I would also advise not using the Sheet2.Range logic, rather work with the sheet names or sheet index

Cheers

Dave

Set sabbr1 = Sheet2.Range("A:A").Find(st, , , xlWhole)
If sabbr1 Is Nothing Then sabbr = sabbr1.Offset(0, 1)

Open in new window

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34868915
wow, typo 2
Set sabbr1 = Sheet2.Range("A:A").Find(st, , , xlWhole)
If Not sabbr1 Is Nothing Then sabbr = sabbr1.Offset(0, 1)

Open in new window

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34868921
Saqib,
Doesn't really seem like much of a reason to me, especially as assignments have a tendency to mutate. ;)
Rory
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 34869146
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
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34869636
Cheers Saqib
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34869662
>By assignment I meant "the task I am working at

That's what Rory meant. Inevitable scope creep :)
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 34869972
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...

;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now