Excel Pivot Table, VBA, change all Field Values to Count Numbers - excel

I have this VBA marco which changes all the field values to Product.
Dim pt As PivotTable
Dim pf As PivotField
Dim ws As Worksheet
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
Application.ScreenUpdating = False
pt.ManualUpdate = True
For Each pf In pt.DataFields
pf.Function = xlProduct
Next pf
pt.ManualUpdate = False
Application.ScreenUpdating = True
Set pf = Nothing
Set pt = Nothing
Set ws = Nothing
End Sub
When when I try changing xlProduct to xlCount.Numbers, the code doesn't work. Any idea how to fix this?

xlCountNums is the proper function.
https://www.thespreadsheetguru.com/blog/2014/8/12/quickly-change-pivot-table-field-calculation-from-count-to-sum

Related

How to add a printer setting into this VBA Code?

I found the below VBA code on the internet which is very helpful, but I would like to add an extra step to change the settings on the printer to "Fit All Columns on One Page". How Can I do that?
Sub PrintFirstFilterItems()
'downloaded from contextures.com
'prints a copy of pivot table
'for each item in
'first Report Filter field
On Error Resume Next
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
Set pf = pt.PageFields(1)
If pf Is Nothing Then Exit Sub
For Each pi In pf.PivotItems
pt.PivotFields(pf.Name) _
.CurrentPage = pi.Name
ActiveSheet.PrintOut 'for printing
'ActiveSheet.PrintPreview 'for testing
Next pi
End Sub
I need the code to change the printer settings to this
I am new on VBA, and all I did was search on google how to change it but nothing worked.
Try the following...
Application.PrintCommunication = False
With ActiveSheet
With .PageSetup
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
End With
Application.PrintCommunication = True

Macro for pivot change filters have no effect

Community, need your help here, other than the fact the macro initially did nothing, I have now an error 1004: Application or object defined error!!
I updated my query to simplify and hoping to correct the no effect result from the macro, but now I made it worst.. any help please
Sub changeFilters()
Dim wsChart As Worksheet: Set wsChart = Sheets("Graph Data")
Dim wsPivot As Worksheet: Set wsPivot = Sheets("PCW_pivot")
Dim pt As PivotTable: Set pt = wsPivot.PivotTables("PCW1")
Dim pf As PivotField: Set pf = pt.PivotFields("[Range].[Geography].[Geography]")
Dim SelGeo As Variant: SelGeo = Sheets("Graph Data").Range("E3").Value
'turn on automatic update / calculation in the Pivot Table
With SelGeo <> "WW"
pt.ManualUpdate = True
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
pf.PivotFilters.Add Type:=xlCaptionEquals, Value1:="[SelGeo]"
pt.RefreshTable
End With
With SelGeo = "WW"
.ClearAllFilters
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

Update Pivot Table Filter With VBA

I am trying to filter a pivot table based on the filter of another pivot table on a different sheet with a different data source.
I have two sheets (Main and Size Actual - Style) and want to filter by "Style". The Main sheet is where I want to choose the styles to filter by and from there I want to have the other sheet update with those selected styles filtered (would help if the filtering occurred with a button). I have changed some code to account for the field (Style) that I want to filter for. Right now I have the Main sheet pivot table resetting to show all values when a filter on style is selected on Size Actual - Style. I need that to be switched and instead of clearing the filter I want it to update with the selected styles from the filter.
Option Explicit
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
On Error Resume Next
Dim wsMain As Worksheet
Dim ws As Worksheet
Dim ptMain As PivotTable
Dim pt As PivotTable
Dim pfMain As PivotField
Dim pf As PivotField
Dim pi As PivotItem
Dim bMI As Boolean
On Error Resume Next
Set wsMain = ActiveSheet
Set ptMain = Target
Application.EnableEvents = False
Application.ScreenUpdating = False
For Each pfMain In ptMain.PageFields
bMI = pfMain.EnableMultiplePageItems
For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
If ws.Name & "_" & pt <> wsMain.Name & "_" & ptMain Then
pt.ManualUpdate = True
Set pf = pt.PivotFields("Style")
bMI = pfMain.EnableMultiplePageItems
With pf
.ClearAllFilters
Select Case bMI
Case False
.CurrentPage = pfMain.CurrentPage.Value
Case True
.CurrentPage = "(All)"
For Each pi In pfMain.PivotItems
.PivotItems("Style").Visible =
pi.Visible
Next pi
.EnableMultiplePageItems = bMI
End Select
End With
bMI = False
Set pf = Nothing
pt.ManualUpdate = False
End If
Next pt
Next ws
Next pfMain
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
I am trying to have the filter on the second sheet update its filter based on the selection on the first sheets style filter.

How to remove all column fields in a Pivot Table ( Power Pivot)?

everyone.
I was using the Sub ClearColumns to clear all field in the column area from a Pivot Table.
Sub ClearColumns()
Dim PT As PivotTable, PF As PivotField
Set PT = ActiveSheet.PivotTables("Tb1")
With PT
For Each PF In .ColumnFields
PF.Orientation = xlHidden
Next PF
End With
Set PT = Nothing
End Sub
But now that i started using PowerPivot, the code doesnt Work. I think the fields are no longer PivotField, but CubeFields. It Is possible to make the same macro above, but for Cubefields? I tried different things, but i cant make it work.
Sub ClearColumnsPP()
Dim PT As PivotTable, PF As CubeFields
Dim strSource As String
Dim strName As String
Set PT = ActiveSheet.PivotTables("Tb1")
With PT
For Each PF In .ColumnFields
strSource = PF.SourceName
strName = PF.Name
PF(strSource, strName).Orientation = xlHidden
Next PF
End With
Set PT = Nothing
End Sub
Thanks!!
You need to check the orientation of the CubeField, and only set to hidden if it is a Column field:
Sub foo()
Dim pt As PivotTable
Dim cf As CubeField
Set pt = ActiveSheet.PivotTables("Tb1")
For Each cf In pt.CubeFields
If cf.Orientation = xlColumnField Then cf.Orientation = xlHidden
Next cf
End Sub

Using "For Each" for apply same filters for all pivot tables in the worksheet

I guess this is an easy question but since I'm new in VBA I just can't figure it out.
I have this code which goes to Pivot table1, select the DATA Pivot field and filter some values for this field.
Sub Multiple_Filtering()
Dim pf As PivotField
Set pf = ActiveSheet.PivotTables("Pivot table1").PivotFields("DATA")
'Enable filtering on multiple items
pf.EnableMultiplePageItems = True
'Must turn off items you do not want showing
pf.PivotItems("201511").Visible = False
pf.PivotItems("201512").Visible = False
pf.PivotItems("201501").Visible = True
pf.PivotItems("201502").Visible = True
End Sub
What I want to do is make a loop which does the same for all pivot tables in all worksheets in the workbook.
I found this piece of code which uses "For each" sentence to sets the data source for all pivot tables in the workbook, but I can't adapt it for my code.
Dim sht As Worksheet
Dim pvt As PivotTable
For Each sht In ThisWorkbook.Worksheets
For Each pvt In sht.PivotTables
pvt.ChangePivotCache _
ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SourceAddress)
pvt.RefreshTable
Next pvt
Next sht
Any ideas?
Sub Multiple_Filtering()
Dim pf As PivotField
Dim sht As Worksheet
Dim pvt As PivotTable
For Each sht In ThisWorkbook.Worksheets
For Each pvt In sht.PivotTables
Set pf = pvt.PivotFields("DATA")
'Enable filtering on multiple items
pf.EnableMultiplePageItems = True
'Must turn off items you do not want showing
pf.PivotItems("201511").Visible = False
pf.PivotItems("201512").Visible = False
pf.PivotItems("201501").Visible = True
pf.PivotItems("201502").Visible = True
Next pvt
Next sht
End Sub
I believe this should integrate the two pieces of code shown there
Sub Multiple_Filtering()
Dim sht As Worksheet
Dim pvt As PivotTable
Dim pf As PivotField
For Each sht In ThisWorkbook.Worksheets
For Each pvt In sht.PivotTables
Set pf = sht.PivotTables(pvt).Pivotfields("DATA")
'Enable filtering on multiple items
pf.EnableMultiplePageItems = True
'Must turn off items you do not want showing
pf.PivotItems("201511").Visible = False
pf.PivotItems("201512").Visible = False
pf.PivotItems("201501").Visible = True
pf.PivotItems("201502").Visible = True
'Admittedly not sure if this is necessary
pvt.RefreshTable
Next pvt
Next sht
End Sub

Resources