Refresh specific pivot table and add measures accordingly
Sub PivotRefresh()
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim pt As PivotTable
Dim ptField(30) As PivotField
Dim Cur(30) As String
Dim i As Byte
Dim wb As Workbook
Set wb = ThisWorkbook
Set ws = wb.Sheets(PivotSht) 'change sheet name accordingly
Set ws1 = wb.Sheets(InputSht) 'change sheet name accordingly
Set pt = ws.PivotTables(1) 'change pivot number accordingly
'assign measures to array
For i = 1 To 30
Cur(i) = ws1.Cells(2, i + 14)
Next i
With pt
.PivotCache.Refresh
For i = 21 To 30
If i <= 24 Then
Set ptField(i) = .AddDataField(.PivotFields(Cur(i) & "2"), Cur(i) & "2" & " ", xlSum)
.InGridDropZones = True
ptField(i).Position = i - 20
.RowAxisLayout xlTabularRow
Else
Set ptField(i) = .AddDataField(.PivotFields(Cur(i) & "3"), Cur(i) & "3" & " ", xlSum)
.InGridDropZones = True
ptField(i).Position = i - 20
.RowAxisLayout xlTabularRow
End If
Next i
End With
Set ws = Nothing
Set ws1 = Nothing
Set pt = Nothing
End Sub
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim pt As PivotTable
Dim ptField(30) As PivotField
Dim Cur(30) As String
Dim i As Byte
Dim wb As Workbook
Set wb = ThisWorkbook
Set ws = wb.Sheets(PivotSht) 'change sheet name accordingly
Set ws1 = wb.Sheets(InputSht) 'change sheet name accordingly
Set pt = ws.PivotTables(1) 'change pivot number accordingly
'assign measures to array
For i = 1 To 30
Cur(i) = ws1.Cells(2, i + 14)
Next i
With pt
.PivotCache.Refresh
For i = 21 To 30
If i <= 24 Then
Set ptField(i) = .AddDataField(.PivotFields(Cur(i) & "2"), Cur(i) & "2" & " ", xlSum)
.InGridDropZones = True
ptField(i).Position = i - 20
.RowAxisLayout xlTabularRow
Else
Set ptField(i) = .AddDataField(.PivotFields(Cur(i) & "3"), Cur(i) & "3" & " ", xlSum)
.InGridDropZones = True
ptField(i).Position = i - 20
.RowAxisLayout xlTabularRow
End If
Next i
End With
Set ws = Nothing
Set ws1 = Nothing
Set pt = Nothing
End Sub
Filter multiple items in Pivot
Sub FilterPivotItems()
Dim PT As PivotTable
Dim PTItm As PivotItem
Dim FiterArr() As Variant
' use this array variable to select the items in the pivot table filter which you want to keep visible
FiterArr = Array("101", "105", "107")
' set the Pivot Table
Set PT = Sheet1.PivotTables("PivotTable1")
' loop through all Pivot Items in "Value" field of the Pivot
For Each PTItm In PT.PivotFields("Value").PivotItems
If Not IsError(Application.Match(PTItm.Caption, FiterArr, 0)) Then ' check if current item is not in the filter array
PTItm.Visible = True
Else
PTItm.Visible = False
End If
Next PTItm
End Sub
0 टिप्पणियाँ
Please do not enter any spam link in the comment box.