Add PivotItems into Values box as Averages based on criteria

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hi there Pivot Table VBA Pros,

Looks like my questions are becoming more challenging (I hope)

I would like to add Pivot Fields to the value box of a pivot table based on the criteria on the Item on another database.

My Master based base has Product infomation (The headings on the database are the product names (eg. Blue cheese 125g), in my second database I have listed all the products by product category (eg Cheese).

I would like to add each Pivot field that is listed as cheese to the Pivot table.

My second database is set up, with products in Sheet4.range("D:D") and the Product category in Sheet4.range("B:B:).

So if the data in Column B = "Cheese", i want the Item listed in Column D to be added to the pivot table.

the Pivot table is located in Sheet7 and its Pivot table 3

The Keyword (Product category is located in sheet7.range("A1")

The format of each item must be Currency.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi there,

I have made some progress since yesturday. However I am now trying to remove all items from the Values field in the PivotTable before the next Catogory is added.

Here is the code that I have created thus far.
Code:
Sub FilterPTDataFieldsFromLookUpTable()
Dim LookUpItemCount As Integer
Dim FieldToAdd As String

LookUpItemCount = WorksheetFunction.CountA(Sheet1.Range("A:A"))

For Each cell In Sheet1.Range("B2:B" & LookUpItemCount)
    If cell.Offset(0, -1).Value = "Cheese" Then
        On Error Resume Next
            FieldToAdd = cell.Value
            With Sheet2.PivotTables("PivotTable2").PivotFields(FieldToAdd)
                .Orientation = xlDataField
                .Position = 1
                .Caption = FieldToAdd & " "
                .Function = xlAverage
                .NumberFormat = "R # ##0.00"
            End With
    End If
        On Error GoTo 0
Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

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