Avoid Formula Error Code

jstiene

Board Regular
Joined
Aug 5, 2005
Messages
222
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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,633
Office Version
  1. 365
Platform
  1. Windows
What is your question?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,633
Office Version
  1. 365
Platform
  1. Windows
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.
 

jstiene

Board Regular
Joined
Aug 5, 2005
Messages
222

ADVERTISEMENT

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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,633
Office Version
  1. 365
Platform
  1. Windows
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
 

jstiene

Board Regular
Joined
Aug 5, 2005
Messages
222

ADVERTISEMENT

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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,633
Office Version
  1. 365
Platform
  1. Windows
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.
 

jstiene

Board Regular
Joined
Aug 5, 2005
Messages
222
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,735
Messages
5,638,057
Members
417,001
Latest member
MSteel

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
Top