Create function based on header name

Defakto

New Member
Joined
Mar 23, 2018
Messages
3
Hi, I have a little problem with Excel, that I am not able to solve.
Let's say I have a table of numbers:
[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]1
[/TD]
[/TR]
[TR]
[TD]2[/TD]
[/TR]
[TR]
[TD]3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[/TR]
[TR]
[TD]5[/TD]
[/TR]
</tbody>[/TABLE]

and I have a table with function names:
[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]sum[/TD]
[TD]average[/TD]
[TD]min[/TD]
[TD]max[/TD]
[TD]count
[/TD]
[/TR]
</tbody>[/TABLE]

Now, I want to create a second row below those function names, that would take the numbers in the first table and perform the function in the second table, without me actually typing each specific function out. I want to use the function names in the table as a reference to automatically create the function.
I tried something like: =A7&"(A1:A5)" where A7 refers to the sum function and the range A1:A5 refers to the numbers. I got a value error.

Thank you in advance for your answers.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You could try this EVALUATE function trick - https://www.vertex42.com/ExcelArticles/evaluate-function.html

In cell A8 put the formula =A7&"(A1:A5)". The result is the text sum(A1:A5). Next, define the name SUMresult and in the Refers To field enter =EVALUATE(A8). Finally in cell A9 enter the formula =SUMresult and the result is 15.

You'll have to repeat this for each function name, defining a different name for each function.

The other, much easier, option is a UDF which calls the VBA Evaluate function:

Code:
Public Function Eval(formulaText As String) As Variant
    Application.Volatile
    Eval = Evaluate(formulaText)
End Function
In cell A8, enter the formula =Eval(A7&"(A1:A5)"), and the result is 15.
 
Last edited:
Upvote 0
If using a UDF, and if you are only interested in those 5 functions, I don't think you need the intermediate step of constructing the function text on the worksheet.

Code:
Function sammc(sFn As String, r As Range) As Double
  sammc = Evaluate(sFn & "(" & r.Address & ")")
End Function

Formula in A8 is copied across.

Excel Workbook
ABCDE
11
22
33
44
55
6
7sumaverageminmaxcount
8153155
Sheet1
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top