Retrieving Formulas within Excel concatenate function

0

I want to parse an excel concatenate statement to find the result of the enclosed expression. Eg: =CONCAT("=1-2-3+7*8") should return 52, and =CONCAT("=AVERAGE(A1,A2)") should return the average of the two fields. Is there any function or any other way to accomplish this in excel?

raghav

Posted 2019-06-24T08:14:49.570

Reputation: 3

Why do you need CONCAT function to return value? You can type *=1-2-3+78** and =AVERAGE(A1,A2) directly. – Lee – 2019-06-24T09:18:38.393

The data I have is in that form. – raghav – 2019-06-24T10:24:22.987

Answers

1

Try the following User Defined Function:

Public Function raghav(rng As Range) As Variant
    Dim s As String

    s = Mid(rng(1).Formula, 2)
    arr = Split(s, Chr(34))
    For Each a In arr
        If Left(a, 1) = "=" Then
            raghav = Evaluate(a)
            Exit Function
        End If
    Next a
End Function

enter image description here

User Defined Functions (UDFs) are very easy to install and use:

  1. ALT-F11 brings up the VBE window
  2. ALT-I ALT-M opens a fresh module
  3. paste the stuff in and close the VBE window

If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the UDF:

  1. bring up the VBE window as above
  2. clear the code out
  3. close the VBE window

To use the UDF from Excel:

=raghav(A1)

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs, see:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!

Gary's Student

Posted 2019-06-24T08:14:49.570

Reputation: 15 540