Hi everyone
I am trying to write a userform where I can change the data field functions for all fields in my pitvot table but I have a problem because I want to pass a function to another function. However I do not know how to convert a string to the function name passed to the second function.
You might understand my problem by studying the two codes below.
The following function changes all data fields in the pivot table.
The following function should pass the pivot function to the above function and execute the command
Where Listbox2 contain string values such as sum or average.
I am probably looking for something like the following Matlab function:
Construct function handle from function name string - MATLAB str2func - MathWorks Nordic
I am trying to write a userform where I can change the data field functions for all fields in my pitvot table but I have a problem because I want to pass a function to another function. However I do not know how to convert a string to the function name passed to the second function.
You might understand my problem by studying the two codes below.
The following function changes all data fields in the pivot table.
Code:
Sub ChangeAllFields(func As Variant)
Dim pt As PivotTable
Dim pf As PivotField
Set pt = ActiveCell.PivotTable
Application.ScreenUpdating = False
pt.ManualUpdate = True
For Each pf In pt.DataFields
pf.Function = func
Next pf
pt.ManualUpdate = False
Application.ScreenUpdating = True
Set pf = Nothing
Set pt = Nothing
End Sub
The following function should pass the pivot function to the above function and execute the command
Code:
Private Sub CommandButton1_Click()
Dim text As String
text = "xl" & ListBox2.value
ChangeAllFields (text)
Unload Me
End Sub
Where Listbox2 contain string values such as sum or average.
I am probably looking for something like the following Matlab function:
Construct function handle from function name string - MATLAB str2func - MathWorks Nordic