1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| Sub CreatePivotTableCompte()
'PURPOSE: Creates a brand new Pivot table on a new worksheet from data in the ActiveSheet
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String
Dim y As Workbook
Set y = Workbooks.Open("Z:\Base_de_données\Base_Para.xlsx")
'Determine the data range you want to pivot
lastrow = y.Sheets("TbCrx").Range("C" & Rows.count).End(xlUp).Row
SrcData = y.Sheets("TbCrx").Name & "!" & Range("B1:F" & lastrow).Address(ReferenceStyle:=xlR1C1)
'Open a new worksheet
Set sht = y.Sheets("Compte")
'Where do you want Pivot Table to start?
StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)
'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable1")
pvt.AddDataField pvt.PivotFields("Montant Final"), , xlSum
pvt.AddDataField pvt.PivotFields("Montant Tarif"), , xlSum
'Add item to the Column Labels
pvt.PivotFields("M/Y").Orientation = xlColumnField
'Add item to the Row Labels
pvt.PivotFields("Compte").Orientation = xlRowField
'Position Item in list
pvt.PivotFields("M/Y").Position = 1
sht.PivotTables("PivotTable1").PivotFields("Somme de Montant Final").Caption = ChrW(931) & "Mnt Final"
sht.PivotTables("PivotTable1").PivotFields("Somme de Montant Tarif").Caption = ChrW(931) & "Tarif"
'Group Date Field by Month and Year
pvt.RowAxisLayout xlTabularRow
Set df = pvt.PivotFields("M/Y")
df.LabelRange.Group _
'j'ai mis False car je veux pas la date automatique, mais comment choisir la date début :01/01/2017 et fin : 31/12/2017?
Start:=False, End:=False, by:=3, Periods:=Array(False, False, False, False, False, True, False)
df.Caption = "Trimestre"
lastrow2 = y.Sheets("Compte").Range("A" & Rows.count).End(xlUp).Row
y.Sheets("Compte").Range("B6:O" & lastrow2).Style = "Currency"
Set sht = y.Sheets("Compte")
sht.Range("B4:J4").replace What:="Trimestre", Replacement:="Q", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
y.Close savechanges:=True
MsgBox ("Task Complete")
End Sub |
Partager