Combine Formulas into one cell

Here is some simple code to combine a selction of formulas into one cell. I have not tested it very much so it's not guaranteed. It will also add a few extra (), than might be needed.

VBA:
  1. Sub CombineFormulas()
  2. Dim objCell As Object
  3. Dim sTemp As String
  4. Dim sOperator As String
  5. Dim rOutPut As range
  6.  
  7. On Error GoTo errorhandel
  8.  
  9. sOperator = InputBox("Operator to use?", "CombineFormulas")
  10.  
  11. For Each cell In Selection
  12. If Len(cell.Cells.Formula)> 1 Then
  13. ''get the formula, adds a "(" , remove the "=", add the closing ")", and puts a "+" on the end
  14. sTemp = sTemp & "(" & Right(cell.Cells.Formula, (Len(cell.Cells.Formula) - 1)) & ")" & sOperator
  15. Else
  16. End If
  17. Next
  18.  
  19. ''Puts an "=" at the start, then gets rid of the last "+",
  20. sTemp = "=" & Left(sTemp, Len(sTemp) - 1)
  21.  
  22. Set rOutPut = Application.InputBox("Put In Cell", Type:=8)
  23.  
  24. ActiveSheet.range(rOutPut.Address).Formula = sTemp
  25.  
  26. Exit Sub
  27.  
  28. errorhandel:
  29. MsgBox "Error: " & vbNewLine _
  30. & Err.Description & vbNewLine _
  31. & Err.Number, _
  32. vbOKOnly, "CombineFormulas"
  33. End Sub

Related posts:

  1. Selection to CSV
  2. Click to install Addin
  3. Fill in empty cells

Submit a Comment