Help with Macro, want to chage to run on active cell instead of whole page

Zoso4

New Member
Joined
Mar 26, 2013
Messages
12
Hi, I am still kind of new to using VBA code and my editing skills are very novice at the moment.
How would I change this Code to run on only the active cell selected and that is all, instead of the whole page?


Sub AddIFERROR()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim xCell As Range
Dim xFormula As String
For Each xCell In Selection
If xCell.HasFormula Then
xFormula = Right(xCell.Formula, Len(xCell.Formula) - 1)
xCell.Formula = "=IFERROR(" & xFormula & ","""")"
End If
Next xCell
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Or Would it be better to add a few more lines to the existing code to check if the IFERROR( AND ,"") Is already in the cell and IF not then do not add. That Way I could add a bunch of new data at a time then run an over all sweep to fill in the IFERROR( AND ,"") to the cells that do not have it.
 
Upvote 0
It should only apply to the selected cell(s). Not the active cell, but the selected cell(s) (those that are highlighted). When I wrote this macro, I did not need it to detect whether IFERROR already existed, but I can certainly add that in.
 
Upvote 0
Code:
Sub AddIFERROR()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim xCell As Range
    Dim xFormula As String
    For Each xCell In Selection
    If xCell.HasFormula Then
        If Right(xCell.Formula, 8) <> "=IFERROR" Then
            xFormula = Right(xCell.Formula, Len(xCell.Formula) - 1)
            xCell.Formula = "=IFERROR(" & xFormula & ","""")"
        End If
    End If
    Next xCell
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
It just dawned on me. I used RIGHT where I should have used LEFT. Sorry about that. Time for more coffee I think...
...
Code:
Sub AddIFERROR()
     Application.ScreenUpdating = False
     Application.Calculation = xlCalculationManual
     Dim xCell As Range
     Dim xFormula As String
     For Each xCell In Selection
        If xCell.HasFormula Then
           If Left(xCell.Formula, 8) <> "=IFERROR" Then
               xFormula = Right(xCell.Formula, Len(xCell.Formula) - 1)
                xCell.Formula = "=IFERROR(" & xFormula & ","""")"
            End If
        End If
     Next xCell
     Application.Calculation = xlCalculationAutomatic
     Application.ScreenUpdating = True
End Sub
</PRE>
 
Upvote 0
Thank you for the reply Gsistek, I will try this out when I am adding in new data from my pivot table tomorrow. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,377
Members
449,097
Latest member
Jabe

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