VBA USERFORMS : Combobox values into Pivot Chart table

sachinns

Board Regular
Joined
Jun 21, 2019
Messages
52
Hi Friends,

ad51ff5b6cdc3b8b08384144402df82a-full.jpg


I have a User form with a comboBox caled "cmbpivotvalue" . When i press Submit/validate button "cmbValidate" in the userform , the values in this comboBox should go to the "VALUES" section on the PivotChartFields (please refer the screenshot) .

Please help me to create code for this.

Thanks in Advance.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The following code assumes that Sheet1 contains the pivot table, and that the pivot table is named PivotTable1. Change these names accordingly. Note that the code will first remove any existing data fields from the pivot table, and then add the pivot field the user selected from the combo box.

Code:
Private Sub cmbValidate_Click()
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pfName As String
    pfName = Me.cmbpivotvalue.Value
    If Len(pfName) = 0 Then
        MsgBox "Please enter a pivot field value, and try again!", vbExclamation
        Exit Sub
    End If
    Set pt = Worksheets("Sheet1").PivotTables("PivotTable1")
    For Each pf In pt.DataFields
        pf.Orientation = xlHidden
    Next pf
    On Error Resume Next
    Set pf = pt.PivotFields(pfName)
    On Error GoTo 0
    If pf Is Nothing Then
        MsgBox "Pivot field not found!", vbExclamation
    Else
        pf.Orientation = xlDataField
    End If
End Sub

Hope this helps!

P.S. No screenshot was posted.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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