I want apply a user defined aggregation function (string-value from a certain cell, like "SUM" or "STDEV") on an array of values. Here's a simplified example. However I don't know how to make the aggregation (last line):
Sub test()
Dim values() As Double
ReDim values(1 To 3)
values(1) = 3.5
values(2) = 5
values(3) = 4.8
Dim aggregate_fn As String
aggregate_fn = "SUM"
Dim result As Double
result = Evaluate("=" & aggregate_fn & "(" & values & ")") ' <-- This doesn't work, but hopefully it's clear what it should do
End Sub
EDIT
My original code is also creating the values array dynamically from a spreadsheet which uses , as decimal sign. This causes an issue with Scott's answer below.
Const datasht = "Daten"
Const aggregate_cell = "G1"
Sub run()
Dim sht As Worksheet
Dim n_rows As Integer
Dim rw As Integer
Application.DecimalSeparator = "."
Set sht = ActiveWorkbook.Worksheets(datasht)
n_rows = sht.Cells(1, 1).CurrentRegion.Rows.Count ' Get range of data
Dim values() As String
ReDim values(1 To n_rows)
For rw = 1 To n_rows
values(rw) = sht.Cells(rw, 1).Value
Next rw
Debug.Print (aggregate(values))
End Sub
Function aggregate(values() As String)
' Get aggregated value
Dim aggregate_fn As String
aggregate_fn = ActiveWorkbook.Worksheets(datasht).Range(aggregate_cell).Value
aggregate = Evaluate("=" & aggregate_fn & "(" & Join(values, ",") & ")") ' <-- doesn't work as intended
End Function


Join(values,",")instead ofvalues