Associa al foglio che contiene le date questo codice
codice:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim ultima As Long
ultima = Range("A65536").End(xlUp).Row
Application.DisplayAlerts = False
Sheets("Grafico Pivot").Delete 'cancello il foglio relativo al grafico senza che mi appaiano messaggi di conferma
Application.DisplayAlerts = True
' verifico che le modifiche al foglio siano relative alla colonna A, quella che contiene le date
If Not Intersect(ActiveCell, Range("A:A")) Is Nothing Then
If WorksheetFunction.CountA(Range("a1:a" & ultima)) = Range("A1:A" & ultima).Count Then
Sheets("Foglio2").Cells.Clear 'cancello tutto il contenuto del foglio in cui andrà la tabella pivot
Dim pt As PivotTable
Dim xlworkbook As Excel.Workbook
Dim WSD As Worksheet
Set WSD = Worksheets("Foglio1") 'foglio che contiene le date
Dim PTOutput As Worksheet
Set PTOutput = Worksheets("Foglio2") 'foglio in cui creare la tabella pivot
Dim PTCache As PivotCache
Dim PRange As Range
Dim ultimariga As Long
ultimariga = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
Set PRange = WSD.Cells(1, 1).Resize(ultimariga, 1)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutput.Cells(1, 1), TableName:="Tabella Pivot")
pt.ManualUpdate = True
pt.AddFields RowFields:=Array("date")
With pt.PivotFields("date")
.Orientation = xlDataField
.Function = xlCount
.Position = 1
End With
pt.ManualUpdate = False
Sheets("Foglio2").Select
ActiveSheet.Range("A2").Select
Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, False, True, False, True)
Charts.Add
With ActiveChart
.SetSourceData Source:=Sheets("Foglio2").Range("A2")
.Location Where:=xlLocationAsNewSheet
End With
With ActiveSheet
.Name = "Grafico pivot" 'rinomino il grafico relativo alla tabella pivot
.Move after:=Worksheets(Worksheets.Count) '... e lo sposto in coda a tutti gli altri fogli
End With
End If
End If
End Sub
Potrai anche rimuovere righe o aggiungere date tra quelle già esistenti avendo sempre la tabella pivot e il relativo grafico aggiornati.