Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

substitute parameters with values in Excel formula

Posted on 2015-02-07
3
Medium Priority
?
67 Views
Last Modified: 2016-02-10
Dear Experts,

I am looking for a function (preferably) that would do the following:
- input: formula (string) or reference to a cell containing formula
- output: input formula (converted to string) but with parameters substituted with values

let's say I have a formula:
=sum(a1;a3;a5)
and cell a1=1, a3=3, a5=5
then the result should be =sum(1;3;5)

if the parameter is a range like in vlookup function, for example:
=vlookup(a1;a:a;3,0)
since second parameter is a range then it should remain unchanged.
the sample result should be in this case like:
=vlookup("pink";a:a;3,0)

is there a way (using VBA?) to achieve it in a relatively simple way?

thank you
Jarek
0
Comment
Question by:ja-rek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 40596601
maybe.  the .FormulaLocal property returns the formula, the issue is in determining which items in the formula are parameters and which are constants.

ill run some tests using the () and commas as separators unless someone comes up with a better solution.
0
 
LVL 31

Assisted Solution

by:hnasr
hnasr earned 800 total points
ID: 40597252
This is not a simple question, so I am showing the idea.

Check this worksheet.
My settings use , as a list separator.
one for simple formual =A1
and another for Sum(A1,A2,A3)
formula-parse.xlsm
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 1200 total points
ID: 40597412
my test as well.  
'ver 1  9.Feb.2015

Function MakeFormula(rng As Range) As String

    Dim ws As Worksheet, rawformula As String
    Dim aa As String, param() As String
    
    Set ws = rng.Worksheet
    
    rawformula = rng.FormulaLocal
    
    'find brackets
    bl = InStr(1, rawformula, "(")
    If bl > 1 Then
        'there is a formula
        MakeFormula = Left$(rawformula, bl)  'the first part before bracket
        
        br = InStrRev(rawformula, ")") - 1
        
        'get parameters
        param = Split(Mid$(rawformula, bl + 1, br - bl), ",")
        MakeFormula = MakeFormula & GetParamValue(param(0))
        For i = 1 To UBound(param)
            MakeFormula = MakeFormula & "," & GetParamValue(param(i))
        Next i
        MakeFormula = MakeFormula & Mid(rawformula, br + 1)
    Else
        MakeFormula = rawformula
    End If
    
    

End Function


Function GetParamValue(param As String) As String
    Dim aa As String, an As Integer, v As Variant
    On Error Resume Next
    Err.Clear
    If (InStr(param, ":")) Then
        'is a range definition itself
        'so return as text
        GetParamValue = param
        Exit Function
    End If
    
    v = Range(param).Value
    If Err > 0 Then
        'wasnt a valid range so return as strin
        GetParamValue = param
     Else
        If IsNumeric(v) Then
            GetParamValue = Str(v)
        ElseIf IsDate(v) Then
            GetParamValue = Format(v, "dd/mmm/yyyy hh:mm:ss")
        Else
            GetParamValue = Chr(34) & v & Chr(34)
        End If
            
        
    End If
End Function

Open in new window

makeparam.xlsm
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question