Excel formulas (e.g. sum, iferror) in VBA

excelstarter1

Board Regular
Joined
Jul 20, 2017
Messages
81
Hi guys,

I am stuck with a little problem. I want to run a macro which adds the iferror function to the selected cell. It does work with the following code, however I dont want to be bound to the "local" formula. E.g. when the macro is used within the organization in Spain or France, the formula should be used/displayed correctly. If a colleague in France opens my macro (in his French Excel Version), the formula does not work because Excel does not recognize the English iferror term.

Code:
selection.FormulaLocal = "=IFERROR(" & Right(selection.FormulaLocal,  Len(selection.FormulaLocal) - 1) & ";" & Chr(34) & "error"  & Chr(34) & ")"

(1) How do I get rid of the FormulaLocal part and tell Excel to insert the "correct" iferror formula, no matter which Excel version is used.

(2) Furthermore, I want to run a macro which checks a selected cell for an iferror formula. So basically if the selected cell starts with =iferror(...;"error") then I want to get rid of the "error") part at the end and replace it by "no value") for example. It is important that only formulas with iferror at the beginning are selected. The iferror formula should not be embedded in a longer formular, e.g. =sum(iferror(...).

Do you have an idea how to do that in VBA??

Thank you very much in advance!

Regards
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I just read something about the WorksheetFunction.IfError or Application.IfError Function. Would that help in my case? I just don't know how to implement it...
 
Upvote 0
To get rid of FormulaLocal all you have to do is:
Change every instance of FormulaLocal to Formula
Change the argument separator from semicolon to comma

Something like
Selection.Formula = "=IFERROR(" & Right(Selection.Formula, Len(Selection.Formula) - 1) & "," & Chr(34) & "error" & Chr(34) & ")"

M.
 
Upvote 0
Remark
Provided the user has Excel 2007 or higher (IFERROR is not available on previous versions) the code above should work. Worked for me (Excel 2010 Brazilian version).

For earlier versions you should use
=IF(ISERROR(formula_here),"error",formula_here)

M.
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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