Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub Toggle_IfError()
- 'Adds/Removes the Iferror function from a set of formulas.
- 'This won't convert =If(iserror(formula),return,formula), instead will wrap the whole thing in _
- an IfError function which would probably never return an error anyway.
- 'It doesn't play nice with multiple selections, but does ignore blank cells.
- Dim rng As Range
- Set rng = Selection
- Dim rows As Integer
- Dim columns As Integer
- Dim Formula As String
- On Error GoTo Error1
- rows = rng.rows.Count
- columns = rng.columns.Count
- For Each c In rng.Cells
- Formula = c.Formula
- If Left(Formula, 1) = "=" Then
- If Left(Formula, 9) = "=IFERROR(" Then
- 'Remove the outer IFERROR formula
- Dim CommaFromRight As Integer
- CommaFromRight = 0
- Dim CommaFound As Boolean
- CommaFound = False
- Do While CommaFound = False ' Find the possition of "," from the right
- If Left(Right(Formula, CommaFromRight), 1) = "," Then
- CommaFound = True
- Else
- CommaFromRight = CommaFromRight + 1
- End If
- Loop
- Formula = "=" & Mid(Formula, 10, Len(Formula) - (9 + CommaFromRight))
- Else
- Dim ErrorValue As String
- If LenB(ErrorValue) = 0 Then
- ErrorValue = InputBox("What would you like to be returned?", "Error Value", 0)
- End If
- Formula = "=IFERROR(" & Right(Formula, Len(Formula) - 1) & "," & ErrorValue & ")"
- End If
- c.Formula = Formula
- End If
- Next
- Exit Sub
- Error1:
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement