iserror in VBA.

G2K

Active Member
Joined
May 29, 2009
Messages
355
Hi,

i have been trying to handle the error in vba for last two days and have tried all the possible option i know but unfortunately not got rid of this error till now. below is the code-

if not iserror(Application.WorksheetFunction.Rank(Cells(a, 24), Range("x:x"))) then

Activecell.value = Application.WorksheetFunction.Rank(Cells(a, 24), Range("x:x"))

else

activecell.value = "NA"

end if

i just want rank value in Activecell and if there is any error or cell is blank in corosponding X column, function should return "NA" for this cell. but given code generates error - "Run time error 1004"

"Unable to get the rank property of the worksheetFunction class"

whenever it encounters a blank cell, otherwise it's working fine.

Please help.........

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:

Code:
    Err.Clear
    On Error Resume Next
    ActiveCell.Value = Application.WorksheetFunction.Rank(Cells(a, 24), Range("x:x"))
    If Err.Number > 0 Then
        ActiveCell.Value = "NA"
    End If
    On Error GoTo 0

Hope it helps
Ryan
 
Upvote 0
Thanks Ryan,

It's working like a charm,

would you please explain why WorksheetFunction.IsError/Iserr or all other error hanling options could not work here.

and why do we need multiple error handler for one line code.

thanks agian for your kind help
 
Upvote 0
When "Application.WorksheetFunction.Rank" returns an error you get an immediate run time error and your code crashes. You have to avoid this crash
by using:
On Error Resume Next

To actually pick up that an error occurred you use Err.Number.
 
Upvote 0
An alternative approach would be as follows:
Code:
    On Error GoTo ErrOccurred
    ActiveCell.Value = Application.WorksheetFunction.Rank(Cells(a, 24), Range("x:x"))
    GoTo NoErr
ErrOccurred:
    ActiveCell.Value = "NA"
NoErr:
    rest of code
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top