Error getting worksheet in Worksheet_PivotTableUpdate - vba

I'm trying to synchronize the filters of 2 pivot table(just from one way, change filters in pivot2 when filters in pivot1 are changed but not the opossite).
But I get this error when I try getting the sheet.
st = Target.Worksheet
Excecution Time Erro 438
object doesnt support this property or method
The pivot in this worksheet might be changed in VBA Code of the event "WorkbookOpen" so it might not run when this worksheet is active, this is the code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
If Target.Name <> "pivot1" Then Exit Sub
Dim st As Worksheet
Dim pivot1 As PivotTable
Dim pivot2 As PivotTable
Set st = Target.Worksheet
Set pivot2 = st.PivotTables("pivot2")
Set pivot1 = st.PivotTables("pivot1")
For Each pf In pivot1.PageFields
If pf.Name <> "Filter1" Then
pivot2.PageFields(pf.Name).CurrentPage = pf.CurrentPage
End If
Next
End Sub

Because a worksheet is an object, you need to use the Set keyword i.e. Set st = Target.Worksheet
On another note, are these PivotTables based on the same data source? If so, no VBA necessary: Just add a Slicer and connect them via right clicking the Slicer and selecting Report Connections.

Related

Combining Excel Slicers and Multiple Pivot Table with VBA

I'm trying to combine a Excel Pivot Table Slicer to control multiple pivot tables form different data tables.
I've found a function do detect and record the Slicer's active selection:
Public Function SlicerSelections(Slicer_Name As String)
FblSlicerSelections = ""
Dim i As Integer
With ActiveWorkbook.SlicerCaches(Slicer_Name)
For i = 1 To .SlicerItems.Count
If .SlicerItems(i).Selected Then
SlicerSelections = SlicerSelections & " " & .SlicerItems(i).Value
End If
Next i
End With
End Function
Now I want to use the results of these function to alter the pivot fields of others pivot tables.
All the pivot tables shares a number of common fields, like primaries keys.
I've already tried to create a single query to combine all the data into a single table, so I could use Slicers to navigate trough the data in different pivot tables, but, as the unique queries has different number of registers for a single set of primary keys, the end result was messy.
How can I use the results of SlicerSelections function to alter the filter parameters (pivot fields) of a Pivot Table and refresh it, so I can use the first pivot table slicer and a macro to command all the pivot tables within a spreadsheet?
Thank you all.
Well, things didn't go as smooth as I imagine.
The procedure works very well with a static data base. I mean, of I don't change the original data sets.
But the data sets have to change, because the data tables will be fulfilled by a query.
First error that apear was the Run Time Error 5.
So I've researched a little bit and understood that the pivotcaches should be reseted in order to run the procedure multiple times.
I've found a code to do that, but now I'm facing the following error: Run time error 1004 - Application Defined or Objected Defined error.
Here is the codes:
Sub Atualiza_Din()
'Dim pvtf As PivotField
Call PivotCacheReset
' Definição das variáveis de controle
Set pvtf_Dias_cen = ActiveSheet.PivotTables("TDDias").PivotFields("Número")
Set pvtf_Dias_per = ActiveSheet.PivotTables("TDDias").PivotFields("PER_Nome")
Set pvtf_Dias_ref = ActiveSheet.PivotTables("TDDias").PivotFields("REF_Nome")
Set pvtf_Dias_tpu = ActiveSheet.PivotTables("TDDias").PivotFields("TPU_Nome")
Set pvtf_Dias_upr = ActiveSheet.PivotTables("TDDias").PivotFields("UPR_Nome")
Set pvtf_Prod_cen = ActiveSheet.PivotTables("TDProd").PivotFields("Número")
Set pvtf_Prod_per = ActiveSheet.PivotTables("TDProd").PivotFields("PER_Nome")
Set pvtf_Prod_ref = ActiveSheet.PivotTables("TDProd").PivotFields("REF_Nome")
Set pvtf_Prod_tpu = ActiveSheet.PivotTables("TDProd").PivotFields("TPU_Nome")
Set pvtf_Prod_upr = ActiveSheet.PivotTables("TDProd").PivotFields("UPR_Nome")
cen = SlicerSelections("SegmentaçãodeDados_Número1") 'Identifica o cenário selecionado
per = SlicerSelections("SegmentaçãodeDados_PER_Nome1") 'Identifica o período selecionado
ref = SlicerSelections("SegmentaçãodeDados_REF_Nome1") 'Identifica a refinaria selecionada
tpu = SlicerSelections("SegmentaçãodeDados_TPU_Nome1") 'Identifica o processo selecionado
upr = SlicerSelections("SegmentaçãodeDados_UPR_Nome1") 'Identifica a unidade selecionada
'MsgBox (ref)
pvtf_Dias_cen.CurrentPage = cen
pvtf_Dias_per.CurrentPage = per
pvtf_Dias_ref.CurrentPage = ref
pvtf_Dias_tpu.CurrentPage = tpu
pvtf_Dias_upr.CurrentPage = upr
pvtf_Prod_cen.CurrentPage = cen
pvtf_Prod_per.CurrentPage = per
pvtf_Prod_ref.CurrentPage = ref
pvtf_Prod_tpu.CurrentPage = tpu
pvtf_Prod_upr.CurrentPage = upr
End Sub
And here is the pivot cache reset:
Sub PivotCacheReset()
' When a data set is pivoted and then some of the underlying data is
' removed, even after a refresh old removed values can still remain in the
' pivot filter. This macro changes the properties of all pivot tables in
' the active workbook, to prevent missing items from appearing, or clear
' items that have appeared. It also resets all pivot table caches in the
' active workbook.
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim iWs As Worksheet
Dim pvt As PivotTable
Dim iProtectState As Boolean
' Loop through each worksheet.
For Each iWs In wb.Worksheets
' If the worksheet is protected, unprotect it. Remember
' it was protected so that it can be re-protected later.
iProtectState = False ' Reset variable that remembers if each sheet is protected.
If iWs.ProtectContents = True Then
' Worksheet is protected.
iWs.Unprotect ' Unprotect the worksheet.
iProtectState = True ' Remember that this worksheet was protected.
End If
' Loop through each pivot table in the sheet.
For Each pvt In iWs.PivotTables
pvt.PivotCache.MissingItemsLimit = xlMissingItemsNone ' Don't allow missing items in the pivot table filters.
pvt.PivotCache.Refresh ' Reset the pivot table cache including any missing items.
Next pvt
' If the worksheet was originally protected, re-protect it.
If iProtectState = True Then iWs.Protect
Next iWs
End Sub
Where I'm making the mistake?

VBA macro to change filters in pivot table

I'm trying do automate a daily report and therefore I want to create two buttons which change the filters of three pivot tables. In detail the buttons shall change the day which is shown. The first filters on yesterday the second one is a reset button do clear all filters and show all days.
The "Resest"-Button is working but the "Yesterday"-Button not.
At the moment the macro looks like that:
Private Sub CommandButton2_Click()
MsgBox ActiveSheet.Range("B1")
With ActiveSheet.PivotTables("Detail_Digital").PivotFields("Tag").CurrentPage = _
ACtiveSheet.Range("B1").Value
End With
End Sub
I've also tried PivotFilters.Add _ , Type:=xlDateYesterday but that isn't working either.
Any suggestions?
Try the code below, it should work, unless your "Date" is formatted differently between the Pivot's data source and Range("B1").
Note: try to avoid using ActiveSheet, instead use referenced objects. In the case below, replace Worksheets("Sheet1") with your sheet's name.
Code
Option Explicit
Private Sub CommandButton2_Click()
Dim PvtTbl As PivotTable
Dim PvtItm As PivotItem
' set the Pivot Table
Set PvtTbl = Worksheets("Sheet1").PivotTables("Detail_Digital")
With PvtTbl
.PivotFields("Tag").ClearAllFilters ' <-- clear all filters to "Tag"
'Debug.Print Worksheets("Sheet1").Range("B1").Value
For Each PvtItm In .PivotFields("Tag").PivotItems
If PvtItm.Name = Worksheets("Sheet1").Range("B1").Value Then
PvtItm.Visible = True
Else
PvtItm.Visible = False
End If
Next PvtItm
End With
End Sub

Update and filter pivot with VBA - Wildcard filter

I want to clear the prevoius filter on pivotfield Invoicenr, update a pivot table, and not show certain items.
I want to show Everything but the items that have a Invoicenr that begins with PO* (seems that * can't be used in VBA?).
Besides this I want to see everything else and the Invoicenr that starts with PO and contains OH.
See my attempt below:
Sub Macro2()
'
' Macro2 Macro
'
ThisWorkbook.RefreshAll
'Worksheets("Pivot").Select
'ActiveSheet.PivotTables("PIVOT1").RepeatAllLabels xlRepeatLabels
ActiveSheet.PivotTables("PIVOT1").PivotFields("Invoicenr"). _
ClearLabelFilters
With ActiveSheet.PivotTables("PIVOT1").PivotFields("invoicenr")
.PivotItems("PO").Visible = False
End With
End Sub
If I'm understanding the conditions correctly, this should get you the results you want for the first case...
Show All Items except ones that begin with "PO" :
Sub ShowAllButPO()
Dim ws As Worksheet
Dim pvtTable As PivotTable
Dim pvtField As PivotField
Dim pvtItem As PivotItem
Set ws = ActiveSheet
Set pvtTable = ws.PivotTables("PIVOT1")
Set pvtField = pvtTable.PivotFields("Invoicenr")
pvtTable.RefreshTable
pvtTable.ClearAllFilters
For Each pvtItem In pvtField.PivotItems
If Left(UCase(pvtItem), 2) = "PO" Then
pvtItem.Visible = False
End If
Next
End Sub
And this should cover the second condition...
Show All Items in "invoicenr" that start with "PO" and also contain "OH" :
Sub ShowOnlyPO()
Dim ws As Worksheet
Dim pvtTable As PivotTable
Dim pvtField As PivotField
Dim pvtItem As PivotItem
Set ws = ActiveSheet
Set pvtTable = ws.PivotTables("PIVOT1")
Set pvtField = pvtTable.PivotFields("Invoicenr")
pvtTable.RefreshTable
pvtTable.ClearAllFilters
For Each pvtItem In pvtField.PivotItems
If Left(UCase(pvtItem), 2) = "PO" And InStr(UCase(pvtItem), "OH") > 0 Then
pvtItem.Visible = True
Else
pvtItem.Visible = False
End If
Next
End Sub
I'm less sure about what you wanted for the second condition. Your wording "i want to see Everything else and the invoicenr that starts with PO and contains "OH"" wasn't completely clear to me.
If you could clarify what you mean by "Everything else and invoicenr that starts with PO.. etc etc" then I can update my code if needed.
Also, if those two code blocks end up getting you what you want, then you could just assign each macro to its own button in your worksheet. That way, you could just toggle the two scenarios without having to open the VBEditor to run the code. If you are unsure how to do this, check out this link
Use this code:
Sub Except_PO()
Dim var As Variant
var = "PO*"
ActiveSheet.PivotTables("Pivot1").PivotFields("Invoicenr").ClearAllFilters
ActiveSheet.PivotTables("Pivot1").PivotFields("Invoicenr").PivotFilters. _
Add Type:=xlCaptionDoesNotEqual, Value1:=var
End Sub
Sub POwithOH()
Dim var As Variant
var = "PO*OH*"
ActiveSheet.PivotTables("Pivot1").PivotFields("Invoicenr").ClearAllFilters
ActiveSheet.PivotTables("Pivot1").PivotFields("Invoicenr").PivotFilters. _
Add Type:=xlCaptionEquals, Value1:=var
End Sub
Then make 2 command buttons with this code
filtering All EXCEPT PO
Private Sub CommandButton1_Click()
Call Except_PO
End Sub
Filtering data starting with PO and contains OH
Private Sub CommandButton2_Click()
Call POwithOH
End Sub
So if you click CommandButton1, your pivot will filter those data that don't start with PO.
And when you click CommandButton2, your pivot will filter all data that starts with PO AND contains OH.

PowerPivot Drill Through Formatting

Does anyone know a way to carry formatting through in a PowerPivot drill through? Whenever I do Drill Throughs on a PowerPivot workbook the report generated does not have the formatting set in PowerPivot, it just has the raw formatting from the table.
For changing the default table colors do this:
Double click to drill down and view the page.
Your tab now says Table Tools..
In Table Styles group click the lower right drop down button to view all styles
At the bottom see "New Table Style..."
Find the check box at the bottom of this dialog (New Table Quick Style) "Set as default table quick style for this document"
You'll have to use macros if you want to do anything else, this how I have accomplished column hiding in the past:
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Set pt = Me.PivotTables("PivotTable1") ' replace with your PT name (see in pivot table options)
For Each pf In pt.PivotFields
If pf.Name = "Field1" Then ' change this to your field name
For Each pi In pf.PivotItems
pi.ShowDetail = True ' True to show, False to Hide
Next pi
End If
Next pf
Another way to accomplish this via macro is putting something like this in the workbook module:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
ans = MsgBox("Format this sheet?", vbYesNo)
If ans = vbYes Then blahh Sh
End Sub
Then put this in the tandard code module:
Sub blahh(sht As Worksheet)
MsgBox "formatting new sheet: " & sht.Name
End Sub
Tested and works with a pivot table, Excel 2007 already adds some styling to the drill down sheet.

Excel VBA or VSTO - How do you loop over Fields on a PivotTable?

Here is some sample VBA code:
Sub Macro1()
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("SomePivotTable")
'Set colOfFields = pt.PivotFields
End Sub
The third line is incomplete/broken. What is the correct way to get access to collection of all the fields in a PivotTable? I need to be able to loop over them. Actual coding is being done in C# VSTO Project.
This works for me (Excel 2003 [11.8146.8202] SP2):
Sub Macro1()
Dim pt As PivotTable
Dim col As PivotFields
Dim c As PivotField
' Name of the pivot table comes from right clicking on the pivot table,
' Table Options..., Name field.
Set pt = ActiveSheet.PivotTables("PivotTable1")
Set col = pt.PivotFields
For Each c In col
Debug.Print c.Name
Next
End Sub
Ok. Found some C#-flavored code ideas from:
http://blogs.msdn.com/andreww/archive/2008/07/25/creating-a-pivottable-programmatically.aspx
// pvtTable is an Excel.PivotTable set earlier in the code
Excel.PivotFields pflds =
(Excel.PivotFields)pvtTable.PivotFields(System.Type.Missing);
foreach (Excel.PivotField pf in pflds)
{
//some code here
}
The trick is passing in the System.Type.Missing to get the "collection" of fields back.