Insert a string into formulas in a range using VBA

Tresfjording

New Member
Joined
Dec 14, 2019
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi....
I have made this macro in VBA:
VBA Code:
Sub ErstattTxt()

'definitions
Dim i As Long
Dim OldFormula As String
Dim OldFormula2 As String
Dim NewFormula As String

'loopstart
i = 1

'grabbing exsisting formula in active cell
OldFormula = ActiveCell.Formula

'Remove the =character in exisiting formula
OldFormula2 = WorksheetFunction.Substitute(OldFormula, "=", "")

'Defining the new formula
NewFormula = "=iferror(" & OldFormula2 & ";"")"

'checking if active cell is not empty
Do While ActiveCell(i, ActiveCell.Value) <> ""

'Insert new parts of old formulas
If ActiveCell(i, ActiveCell).Formula = OldFormula Then

'write new formula to cell
ActiveCell.Formula = NewFormula 'heres were it all goes wrong

'move cursor one cell down
ActiveCell.Offset(1).Select
End If

'Repeat 5 times (or column down)
If i = 5 Then End

i = i + 1

Loop

End Sub

I get no errors, but the formula in active cell do not change.

What I am trying to do is to add =iferror( to the beginning, and ;"") to the end, of all formulas in a column of cells. The formulas are all different.
The problem is that the variable NewFormula do not end up as a formula in active cell.

What am I doing wrong?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I'm not sure what you are trying to do with this bit?

Code:
ActiveCell(i, ActiveCell.Value)

You could try something like this:

VBA Code:
Sub AddIfError()
    Dim rCell                 As Range
    Dim rFormulas             As Range

    If Selection.Count = 1 Then
        If Selection.HasFormula Then Set rFormulas = Selection
    Else
        On Error Resume Next
        Set rFormulas = Selection.SpecialCells(xlCellTypeFormulas)
        On Error GoTo 0
    End If

    If rFormulas Is Nothing Then
        MsgBox "No formulas in selection"
    Else
        For Each rCell In rFormulas
            If UCase$(Left$(rCell.Formula, 8)) <> "=IFERROR" Then
                If rCell.HasArray Then
                    rCell.CurrentArray.FormulaArray = "=IFERROR(" & Mid$(rCell.FormulaArray, 2) & ","""")"
                Else
                    rCell.Formula2 = "=IFERROR(" & Mid$(rCell.Formula2, 2) & ","""")"
                End If
            End If
        Next rCell
    End If
End Sub

Just select the cells you want before running it.
 
Upvote 1
Solution

Forum statistics

Threads
1,224,809
Messages
6,181,076
Members
453,020
Latest member
mattg2448

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