Sub CreaPivot()
Worksheets("Foglio2").Select
Dim x, y
y = Range("B3:G3").Address
x = Range("B3").End(xlDown).Address
Range(y, x).Select
Sheets.Add
' Nella seguente istruzione non so come impostare SourceData in modo che accolga la selezione dinamica che ho appena selezionato
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Foglio2!R3C2:R12C7", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Foglio5!R3C1", TableName:="Tabella_pivot2", _
DefaultVersion:=xlPivotTableVersion12
Sheets("Foglio5").Select
Cells(3, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("Tabella_pivot2").PivotFields("SOCIETA'")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("Tabella_pivot2").PivotFields("VOCE DI COSTO")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Tabella_pivot2").PivotFields("MESE")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("Tabella_pivot2").AddDataField ActiveSheet.PivotTables( _
"Tabella_pivot2").PivotFields("IMPORTO (€)"), "Somma di IMPORTO (€)", xlSum
Cells.Select
With Selection
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Font
.Name = "Segoe UI"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Selection.Font.Size = 11
Selection.Font.Size = 12
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
Selection.RowHeight = 24.75
Range("B5:E11").Select
With ActiveSheet.PivotTables("Tabella_pivot2").PivotFields( _
"Somma di IMPORTO (€)")
.NumberFormat = "€ #.##0"
End With
Range("A1").Select
End Sub
|