VBA to add IFERROR to all selected cells

  • Hi all,


    I'm trying to add IFERROR to all cells within a selection, but only if the cell does not already have IFERROR. I have a VBA code that does that, but unfortunately it takes hours to run as it loops through 1.5 million cells. Also, I always have to change the range within the Macro, but I would prefer a more 'flexible' method where it checks all cells within a current selection on a certain sheet. Can anyone help me simplify/speed up the code? Also, I'm using Excel 2016 and noticed that my codes slowed down even more when using below code compared to Excel 2010 (1~ hour to now 3~ hours roughly).


  • Re: VBA to add IFERROR to all selected cells


    Below is the code I use to speed up all my Macros. Does anyone know why it is significantly slower in Excel 2016?


  • Re: VBA to add IFERROR to all selected cells


    The first thing I noticed was an error in your OptimizeCode_Begin procedure

    Code
    Application.Calculation = xlCalculationAutomatic


    should be

    Code
    Application.Calculation = xlCalculationManual


    If you set calulation to automatic that every formula is going to refresh for every iteration of your loop in the IFERRTOALLCELLS procedure, that will have a tremendous affect on code run time.


    You can further speed up the code by using an array based code rather than an object based code.


    Try these


    Also, in your code, even though Screen Updating is turned off in the OptimizeCode_Begin procedure, it will automatically be turned back on again when that procedure finishes. You would need to turn it off in the IFERRORTOALLCELLS procedure immediately after calling OptimizeCode_Begin. Using an array based code the screen is only updated once, when the array is posted back to the sheet, so there is no need to turn screen updating off.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: VBA to add IFERROR to all selected cells


    Impressive!! Just tested it, it reduced the run time from 2 hours to 10 seconds! Thanks!!!


    Is there a way to change the VBA code slightly, so that it only applies IFERROR to a current selection rather than having it to change within the Macro every time (changing sheet name and array etc.)? I had something similar in another code (see below) I used before, but don't know how to apply it to the above.


  • Re: VBA to add IFERROR to all selected cells


    Try this

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!