Macro for Iferror

maheshrk81

Board Regular
Joined
Jan 5, 2010
Messages
153
Hi All,

I have a sheet with soo many formulas. but now i need to edit all the formulas with IFERROR, bcoz in few place i am getting #NA or #DVD. Now i need to change those values as 0(zero) with IFERROR formula.

I am using this below code but that is makin my cell empty.
Sub ErrorTrapAddDDL()

' Adds =If(IsError() around formulas

Dim cel As Range
Dim rng As Range
Dim Check As String

Const Equ As String = "=IF(ISERROR(_x) ,"""", _x)"

Check = Left$(Equ, 12) & "*" ' Check for =IF(ISERROR(

On Error Resume Next

Set rng = Selection.SpecialCells(xlFormulas, 23)
If rng Is Nothing Then Exit Sub

With WorksheetFunction
For Each cel In rng
If Not cel.Formula Like Check Then
cel.Formula = .Substitute(Equ, "_x", Mid$(cel.Formula, 2))
End If
Next
End With
End Sub


Kindly help me out..

Please....

Mahi
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Please be a bit more patient next time, 12 minutes is hardly a realistic response time.

Anyhow, see if this works better:
Code:
Sub ErrorTrapAddDDL()

    ' Adds =If(IsError() around formulas

    Dim cel As Range
    Dim rng As Range
    Dim Check As String

    Const Equ As String = "=IF(ISERROR(_x) ,"""", _x)"

    Check = Left$(Equ, 12) & "*" ' Check for =IF(ISERROR(

    On Error Resume Next

    Set rng = Selection.SpecialCells(xlFormulas, 23)
    If rng Is Nothing Then Exit Sub

    With WorksheetFunction
        For Each cel In rng
            If Not cel.Formula Like Check Then
                cel.Formula = .Substitute(Equ, "_x", Right(cel.Formula, Len(cel.Formula) - 1))
            End If
        Next
    End With
End Sub
 
Upvote 0
That code works fine for me, wrapping the existing formulas in ISERROR etc.

You say you now get empty cells?

Are you sure they are empty and it's not that the new formula is actully returning ""?
 
Upvote 0
Am sorry for being in hurry......
The code is not working... Its not yet all performing anything.
I need zero in the place of #NA and #DVD
Please help me..
 
Upvote 0
If you remove On Error Resume Next what errors, if any, do you get?
 
Upvote 0
I have checked with that but not performing...

The First qoute is working fine but that displays the empty cell whereas i need 0(zero) in that place.
Means where ever the formula show as #NA,#DVD then it should show 0 (zero) at that palce.
 
Upvote 0
Oh, that's what you meant. Replace:
Rich (BB code):
Const Equ As String = "=IF(ISERROR(_x) ,"""", _x)"
With:
Rich (BB code):
Const Equ As String = "=IF(ISERROR(_x) ,0 , _x)"
 
Upvote 0
So the code is working, but your formula is wrong?:)

Moonfish's change should fix that.
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,822
Members
452,946
Latest member
JoseDavid

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