Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Toggle_IfError()
  2.  
  3.  
  4.  
  5.     'Adds/Removes the Iferror function from a set of formulas.
  6.  
  7.     'This won't convert =If(iserror(formula),return,formula), instead will wrap the whole thing in _
  8.  
  9.        an IfError function which would probably never return an error anyway.
  10.  
  11.     'It doesn't play nice with multiple selections, but does ignore blank cells.
  12.  
  13.     Dim rng As Range
  14.  
  15.     Set rng = Selection
  16.  
  17.     Dim rows As Integer
  18.  
  19.     Dim columns As Integer
  20.  
  21.     Dim Formula As String
  22.  
  23.    
  24.  
  25.     On Error GoTo Error1
  26.  
  27.    
  28.  
  29.    
  30.  
  31.     rows = rng.rows.Count
  32.  
  33.     columns = rng.columns.Count
  34.  
  35.    
  36.  
  37.     For Each c In rng.Cells
  38.  
  39.        
  40.  
  41.             Formula = c.Formula
  42.  
  43.            
  44.  
  45.             If Left(Formula, 1) = "=" Then
  46.  
  47.            
  48.  
  49.                 If Left(Formula, 9) = "=IFERROR(" Then
  50.  
  51.                     'Remove the outer IFERROR formula
  52.  
  53.                     Dim CommaFromRight As Integer
  54.  
  55.                     CommaFromRight = 0
  56.  
  57.                     Dim CommaFound As Boolean
  58.  
  59.                     CommaFound = False
  60.  
  61.                    
  62.  
  63.                     Do While CommaFound = False ' Find the possition of "," from the right
  64.  
  65.                    
  66.  
  67.                         If Left(Right(Formula, CommaFromRight), 1) = "," Then
  68.  
  69.                             CommaFound = True
  70.  
  71.                         Else
  72.  
  73.                             CommaFromRight = CommaFromRight + 1
  74.  
  75.                         End If
  76.  
  77.                                        
  78.  
  79.                     Loop
  80.  
  81.                    
  82.  
  83.                     Formula = "=" & Mid(Formula, 10, Len(Formula) - (9 + CommaFromRight))
  84.  
  85.                                    
  86.  
  87.                 Else
  88.  
  89.                     Dim ErrorValue As String
  90.  
  91.                    
  92.  
  93.                     If LenB(ErrorValue) = 0 Then
  94.  
  95.                                    
  96.  
  97.                     ErrorValue = InputBox("What would you like to be returned?", "Error Value", 0)
  98.  
  99.                                    
  100.  
  101.                     End If
  102.  
  103.                    
  104.  
  105.                     Formula = "=IFERROR(" & Right(Formula, Len(Formula) - 1) & "," & ErrorValue & ")"
  106.  
  107.                    
  108.  
  109.                 End If
  110.  
  111.                        
  112.  
  113.                 c.Formula = Formula
  114.  
  115.            
  116.  
  117.             End If
  118.  
  119.         Next
  120.  
  121.    
  122.  
  123.     Exit Sub
  124.  
  125.    
  126.  
  127. Error1:
  128.  
  129.    
  130.  
  131. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement