Hi guys! Again i need your experience
I have one sheet ("DATA") where i create 4 single column tables ("TYEAR" - [Year], "TWEEKNO" - [Week #], "MAIN CONTRACTOR" - [Main Contractor]).
I have another 2 sheets, each having a table. Tables are different but both have column "Year", "Week #", "Main Contractor"
I created relationship between tables based on this 3 columns.
On another sheet i insert 2 pivot table which have both as filter these columns. When i manually filter the 1st pivot table the 2nd changes automatically. (which is good ... that's what i wanted)
The problem is that i want to filter the 1st pivot table (and of course the 2nd to change as well) based on a cell value.
I have used the code below but is not working. Which is the problem?
Thank you very much!
'PIVOT TABLE
' Variable Declaration
Dim pt As PivotTable
Dim ptitemmc As PivotItem
Dim ptitemy As PivotItem
Dim ptitemwk As PivotItem
Dim FieldMC As PivotField
Dim FieldYEAR As PivotField
Dim FieldWEEK As PivotField
Dim NewMC As String
Dim NewYEAR As String
Dim NewWEEKa As String
' Assign the starting variable values
Set pt = Worksheets("PVT STATS").PivotTables("PivotTable1")
pt.RefreshTable
Set FieldYEAR = pt.PivotFields("YEAR")
Set FieldWEEK = pt.PivotFields("WEEK #")
Set FieldMC = pt.PivotFields("MAIN CONTRACTOR")
NewYEAR = Worksheet4.Range("A6").Value
NewWEEK = Worksheet4.Range("C6").Value
NewMC = Worksheet4.Range("D6").Value
' Delete ghost PivotItems
For Each ptitemy In FieldYEAR.PivotItems
On Error Resume Next
ptitemy.Delete
On Error GoTo 0
Next ptitemy
For Each ptitemwk In FieldWEEK.PivotItems
On Error Resume Next
ptitemwk.Delete
On Error GoTo 0
Next ptitemwk
For Each ptitemmc In FieldMC.PivotItems
On Error Resume Next
ptitemmc.Delete
On Error GoTo 0
Next ptitemmc
' Test for valid filter value
On Error GoTo InvalidFilter1
FieldYEAR.PivotItems(NewYEAR).Visible = True
On Error GoTo 0
On Error GoTo InvalidFilter1
FieldWEEK.PivotItems(NewWEEK).Visible = True
On Error GoTo 0
On Error GoTo InvalidFilter1
FieldMC.PivotItems(NewMC).Visible = True
On Error GoTo 0
' Loop though PivotItems
For Each ptitemy In FieldYEAR.PivotItems
If ptitemy = NewYEAR Then
ptitemy.Visible = True
Else
ptitemy.Visible = False
End If
Next ptitemy
For Each ptitemwk In FieldWEEK.PivotItems
If ptitemwk = NewWEEK Then
ptitemwk.Visible = True
Else
ptitemwk.Visible = False
End If
Next ptitemwk
For Each ptitemmc In FieldMC.PivotItems
If ptitemmc = NewMC Then
ptitemmc.Visible = True
Else
ptitemmc.Visible = False
End If
Next ptitemmc
Sheet4.Cells(6, 12) = Sheet8.Cells(12, 1).Value
Sheet4.Cells(6, 13) = Sheet8.Cells(12, 2).Value
Sheet4.Cells(6, 11) = Sheet8.Cells(12, 3).Value
Sheet4.Cells(6, 10) = Sheet8.Cells(12, 4).Value
' Stop the code so it doesn't do the InvalidFilter bit
' This runs if there was an error in the test
InvalidFilter1:
Sheet4.Cells(6, 10) = 0
Sheet4.Cells(6, 11) = 0
Sheet4.Cells(6, 13) = 0
Sheet4.Cells(6, 12) = 0
__________________________________________________________________
POST MOVED BY MOD
I have one sheet ("DATA") where i create 4 single column tables ("TYEAR" - [Year], "TWEEKNO" - [Week #], "MAIN CONTRACTOR" - [Main Contractor]).
I have another 2 sheets, each having a table. Tables are different but both have column "Year", "Week #", "Main Contractor"
I created relationship between tables based on this 3 columns.
On another sheet i insert 2 pivot table which have both as filter these columns. When i manually filter the 1st pivot table the 2nd changes automatically. (which is good ... that's what i wanted)
The problem is that i want to filter the 1st pivot table (and of course the 2nd to change as well) based on a cell value.
I have used the code below but is not working. Which is the problem?
Thank you very much!
'PIVOT TABLE
' Variable Declaration
Dim pt As PivotTable
Dim ptitemmc As PivotItem
Dim ptitemy As PivotItem
Dim ptitemwk As PivotItem
Dim FieldMC As PivotField
Dim FieldYEAR As PivotField
Dim FieldWEEK As PivotField
Dim NewMC As String
Dim NewYEAR As String
Dim NewWEEKa As String
' Assign the starting variable values
Set pt = Worksheets("PVT STATS").PivotTables("PivotTable1")
pt.RefreshTable
Set FieldYEAR = pt.PivotFields("YEAR")
Set FieldWEEK = pt.PivotFields("WEEK #")
Set FieldMC = pt.PivotFields("MAIN CONTRACTOR")
NewYEAR = Worksheet4.Range("A6").Value
NewWEEK = Worksheet4.Range("C6").Value
NewMC = Worksheet4.Range("D6").Value
' Delete ghost PivotItems
For Each ptitemy In FieldYEAR.PivotItems
On Error Resume Next
ptitemy.Delete
On Error GoTo 0
Next ptitemy
For Each ptitemwk In FieldWEEK.PivotItems
On Error Resume Next
ptitemwk.Delete
On Error GoTo 0
Next ptitemwk
For Each ptitemmc In FieldMC.PivotItems
On Error Resume Next
ptitemmc.Delete
On Error GoTo 0
Next ptitemmc
' Test for valid filter value
On Error GoTo InvalidFilter1
FieldYEAR.PivotItems(NewYEAR).Visible = True
On Error GoTo 0
On Error GoTo InvalidFilter1
FieldWEEK.PivotItems(NewWEEK).Visible = True
On Error GoTo 0
On Error GoTo InvalidFilter1
FieldMC.PivotItems(NewMC).Visible = True
On Error GoTo 0
' Loop though PivotItems
For Each ptitemy In FieldYEAR.PivotItems
If ptitemy = NewYEAR Then
ptitemy.Visible = True
Else
ptitemy.Visible = False
End If
Next ptitemy
For Each ptitemwk In FieldWEEK.PivotItems
If ptitemwk = NewWEEK Then
ptitemwk.Visible = True
Else
ptitemwk.Visible = False
End If
Next ptitemwk
For Each ptitemmc In FieldMC.PivotItems
If ptitemmc = NewMC Then
ptitemmc.Visible = True
Else
ptitemmc.Visible = False
End If
Next ptitemmc
Sheet4.Cells(6, 12) = Sheet8.Cells(12, 1).Value
Sheet4.Cells(6, 13) = Sheet8.Cells(12, 2).Value
Sheet4.Cells(6, 11) = Sheet8.Cells(12, 3).Value
Sheet4.Cells(6, 10) = Sheet8.Cells(12, 4).Value
' Stop the code so it doesn't do the InvalidFilter bit
' This runs if there was an error in the test
InvalidFilter1:
Sheet4.Cells(6, 10) = 0
Sheet4.Cells(6, 11) = 0
Sheet4.Cells(6, 13) = 0
Sheet4.Cells(6, 12) = 0
__________________________________________________________________
POST MOVED BY MOD
Last edited by a moderator: