Option Explicit
Sub Nomesub()
Dim wb As Workbook
Dim ws As Worksheet
Dim x As Long
Dim rngC As Range, cella As Range
Dim oPVT As PivotTable
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
For Each oPVT In ws.PivotTables
oPVT.TableRange2.Clear
Next oPVT
With ws
x = .Range("A" & .Rows.Count).End(xlUp).Row
Set rngC = .Range(.Cells(1, 1), .Cells(x, 2))
'''''''''''''''''''''''''''''''''''''''''''''
Call Modulo1.Pivot_Saldat(ws, rngC) '''RICHIAMABILE IN QUALUNQUE MOMENTO
'''''''''''''''''''''''''''''''''''''''''''''
Set rngC = Nothing
End With
Set ws = Nothing
Set wb = Nothing
End Sub
Sub Pivot_Saldat(ByVal wsDati As Worksheet, rngDati As Range)
Dim oPvtCch As PivotCache
Dim oPvtTbl As PivotTable
Dim ptField As PivotField
Dim ptItem As PivotItem
Set oPvtCch = wsDati.Parent.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngDati) 'crea cache per tabella dal range
Set oPvtTbl = oPvtCch.CreatePivotTable(wsDati.Cells(2, 7)) 'crea pivot in nuovo foglio
With oPvtTbl
.RowAxisLayout xlOutlineRow
.Name = wsDati.Name
.PivotFields(1).Orientation = xlRowField 'Lato
.PivotFields(1).Position = 1 'Lato come primo elemento
For Each ptItem In .PivotFields(1).PivotItems
If ptItem.Caption = "(blank)" Then ptItem.Visible = False
Next
.AddDataField .PivotFields(2), "Importo", xlSum 'Somma importo
.SubtotalLocation xlAtTop
For Each ptField In .RowFields 'elimino i subtotali
If ptField.Name <> "Lato" Then
On Error Resume Next
ptField.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
End If
Next ptField
End With
Set rngDati = Nothing
Set oPvtCch = Nothing
Set oPvtTbl = Nothing
End Sub
|