Pivot table function from a variable

heideway

New Member
Joined
Jul 18, 2011
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi I'm trying to use the value of a variable that I get from a ComboBox to define the function that I want a PivotTable to display. I get the "type mismatch" error, but the watch shows that the value matches what I selected from the drop-down. I know about enough VBA for Excel to get myself into trouble - which I clearly have and I would appreciate some guidance on how to correct.
Thanks in advance, Pete
Excel Function Question.png
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
It looks to me that the enum xlCount is being used as a string.
Replace you df.Function = line with the below, and if that works the fact it was a string was the issue.
Someone might be able to give you a better way to convert the enum as a string to its value. The normal value conversion didn't work.

VBA Code:
    Dim strEnum As String
    strEnum = frmPivotOptions.cbxOperation.Value

    Select Case strEnum
        Case "xlSum"
            df.Function = xlSum
        Case "xlCount"
            df.Function = xlCount
        Case "xlAverage"
            df.Function = xlAverage
    End Select
 
Upvote 0
Hi Alex,

Thanks for your feedback, but I get the same error, even though I thought that you approach would work. Something to do with the string value that df.Function does not like.

Regards,
Pete
 
Upvote 0
I'd use a two column combobox with the actual numeric value stored in the second column.
 
Upvote 0
If for some reason Rory's suggestion doesn work for you. The select case method worked for me.
Perhaps try adding the debug.print line to see if the values are true and/or F8 through the code to see it performs the right case statement.

Rich (BB code):
Sub PivotFunctionChange()

    Dim pt As PivotTable
    Dim df As PivotField
    Dim sFunction As String
    
    'sFunction = "xlCount"                                       ' My test value worked
    sFunction = frmPivotOptions.cbxOperation.Value
    Debug.print frmPivotOptions.cbxOperation.Value = "xlCount" ' Maybe try this - s/be true

    For Each pt In ActiveSheet.PivotTables
        For Each df In pt.DataFields
            Select Case sFunction
                Case "xlSum"
                    df.Function = xlSum
                Case "xlCount"
                    df.Function = xlCount
                Case "xlAverage"
                    df.Function = xlAverage
            End Select
        Next df
    Next pt

End Sub
 
Upvote 1
Thanks Alex,

Perfect - works great.

Really appreciate the work you guys do with helping us dummies.

Regards,
Pete
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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