[Example Code] Formula Nesting/Embeding / whatever ...

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
i needed something to put multiple formulas or values in another formula so here is the result.
select a non-blank cell
when start inserting a new formula use xxx to indicate where the old one should stay.
it is possible to make a simple equation also but first (sorry about this) a function must be selected then edit directly in the Formula bar as you like.

Code:
Sub WrapInFormula()
'Allows you to select a new formula. The old one is placed where you put "xxx" in the new one.
' This action is applied to each cell in the selected range
Dim oldxxxrng As String
oldxxxrng = IfNamedRangeExistsAddress("xxx")
If oldxxxrng = "" Then
    Application.Names.Add "xxx", "=" & ActiveSheet.Name & "!$A$1"
Else
    ActiveWorkbook.Names("xxx").RefersTo = "=" & ActiveSheet.Name & "!$A$1"
End If

Dim oldformula As String, oldformula1 As String 'Ins As Variant,
Dim newformula As String, newformula_cell As String
Dim rngcell As Range ', rngsel As Range

oldformula1 = ActiveCell.Formula
If InStr(1, oldformula1, "=") = 1 Then
    oldformula = "(" & Mid(oldformula1, 2) & ")"
Else
    oldformula = oldformula1
End If
ActiveCell.ClearContents
Application.DisplayFormulaBar = False
Application.DisplayFormulaBar = True
ActiveCell.FunctionWizard
If Not ActiveCell.Formula = "" Then
    newformula = ActiveCell.Formula
    ActiveCell.Formula = oldformula1
Else
    ActiveCell.Formula = oldformula1
    If oldxxxrng = "" Then
        Application.Names.Item("xxx").Delete
    Else
        ActiveWorkbook.Names("xxx").RefersTo = oldxxxrng
    End If
    Exit Sub
End If
i = 1

For Each rngcell In Selection.Cells
    oldformula1 = rngcell.Formula
    If oldformula1 = "" Then GoTo ThisIsBlankSoSkipIt
    If InStr(1, oldformula1, "=") = 1 Then
        oldformula = "(" & Mid(oldformula1, 2) & ")"
    Else
        oldformula = oldformula1
    End If
    newformula_cell = Replace(newformula, "xxx", oldformula)
    rngcell.Formula = newformula_cell
ThisIsBlankSoSkipIt:
i = i + 1
Next rngcell
If oldxxxrng = "" Then
    Application.Names.Item("xxx").Delete
Else
    ActiveWorkbook.Names("xxx").RefersTo = oldxxxrng
End If
End Sub
it turned out it can be useful to build complex formulas, so i tried to make it as generic as i could.

any comments are appreciated.
all questions will be answered.
feel free to use it if you find it useful :biggrin: (hope someone will)

chrees to all
 

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,806
Messages
5,513,515
Members
408,955
Latest member
BodanKaraoke

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top