Avoid Formula Error Code

jstiene

Board Regular
Joined
Aug 5, 2005
Messages
223
I made a short sub for avoiding formula errors in a column. It assumes your cursor is in the header column of the column you want to handle.
But header row and column can be replaced by a hardcoded number you want to use
It basically turns something like
=G2/H2

into

=IFERROR(G2/H2,"")

Private Sub AvoidErrors()
Dim i As Long, Val1 As String, Val2 As String
Dim Lrow As Long, Ccol As Long, Hrow As Integer, L1 As Integer
'add error handling to formula
Hrow = Selection.Row
Ccol = Selection.Column
Lrow = Cells(Rows.Count, Ccol).End(xlUp).Row
For i = Hrow + 1 To Lrow
On Error Resume Next 'Not sure what to do with blank cells so I'm skipping
Val1 = Cells(i, Ccol).Formula
L1 = Len(Val1)
Val2 = "=IfError(" & Right(Val1, L1 - 1) & "," & Chr(34) & Chr(34) & ")"
Cells(i, Ccol).Formula = Val2
Next i
MsgBox "Done"
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Ok. But I would advise to use that with care.
It could destroy any array entered formulae, also it can cause problems for anyone who has dynamic array formulae.
 
Upvote 0
You should make a place on the site for people's code that is interesting. Not full aps.
I don't even know what a dynamic array formula is. One is code the other front end.
 
Upvote 0
I was just highlighting potential problems, for anyone who finds the code & tries to use it.
Otherwise they could end up destroying their workbook.

Also when posting code please use code tags
 
Upvote 0
I was just highlighting potential problems, for anyone who finds the code & tries to use it.
Otherwise they could end up destroying their workbook.

Also when posting code please use code tags
Almost anything anyone has ever posted to this group could screw up a workbook under certain circumstances. It is a given suggestions and code are tested in a copy or after a workbook is saved. Which is why I almost never use the save command in code.
Manual verification it works is always recommended.
 
Upvote 0
Almost anything anyone has ever posted to this group could screw up a workbook under certain circumstances.
Firstly, I would have to strongly disagree with this blanket assessment.
Secondly starting a thread & effectively saying "Use this to add IFERROR to all your formulae" is not the same as posting code to help with a specific problem.
You seem to have taken offence with my comments (where none was intended), if you cannot handle people pointing out potential problems with your code, I would suggest you don't post it.
 
Upvote 0
I am just saying any code that alters values can screw up a workbook. Which is most code. And no, I didn't take offence at anything.
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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