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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
Option Explicit
Public EcartsProduits() As Variant
Sub CreationTcdProduit()
Dim ShEnsemble As Worksheet, ShTcd As Worksheet
Dim DerniereLigne As Long, DerniereColonne As Long, ColProduit As Long, I As Long, IndexMatrice As Long
Dim AireTableau As Range, AireProduit As Range, AireSemaines As Range
Dim Pvc As PivotCache
Dim Pvt As PivotTable
Set ShEnsemble = Sheets("Ensemble")
With ShEnsemble
DerniereLigne = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
ColProduit = .UsedRange.SpecialCells(xlCellTypeLastCell).Column + 1
.Cells(10, ColProduit) = "Produit"
Set AireProduit = .Range(.Cells(11, ColProduit), .Cells(DerniereLigne, ColProduit))
With AireProduit
.Formula2Local = "=E11&"" ""&C11&"" ""&I11"
.Copy
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.EntireColumn.AutoFit
End With
Set AireTableau = .Range("A10").CurrentRegion
End With
' Vérification de la présence d'au moins 1 table pivotcache. Sinon création du PivotCache
'----------------------------------------------------------------------------------------
With ActiveWorkbook
If .PivotCaches.Count = 0 Then
Set Pvc = .PivotCaches.Create(SourceType:=xlDatabase, SourceData:=AireTableau.Address)
Else
Set Pvc = .PivotCaches(1)
End If
Pvc.MissingItemsLimit = xlMissingItemsNone
End With
Set ShTcd = Sheets.Add
Set Pvt = Pvc.CreatePivotTable(TableDestination:=ShTcd.Cells(3, 1), TableName:="TCD1")
With Pvt
With .PivotFields("Semaine")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Produit")
.Orientation = xlRowField
.Position = 1
End With
.AddDataField .PivotFields("02 Code fonction lien vehicule"), "Nombre de Produit", xlCount
.ColumnGrand = False
.RowGrand = False
Set AireSemaines = Pvt.DataBodyRange
End With
Erase EcartsProduits
IndexMatrice = 0
For I = 1 To AireSemaines.Columns(1).Cells.Count
With AireSemaines.Columns(1).Cells(I)
If Val(.Value) <> Val(.Offset(0, 1)) Then
ReDim Preserve EcartsProduits(4, IndexMatrice)
EcartsProduits(0, IndexMatrice) = Split(.Offset(0, -1), " ")(0)
EcartsProduits(1, IndexMatrice) = Split(.Offset(0, -1), " ")(1)
EcartsProduits(2, IndexMatrice) = Split(.Offset(0, -1), " ")(2)
EcartsProduits(3, IndexMatrice) = Val(.Value)
EcartsProduits(4, IndexMatrice) = Val(.Offset(0, 1))
IndexMatrice = IndexMatrice + 1
End If
End With
Next I
If IndexMatrice > 0 Then
For IndexMatrice = LBound(EcartsProduits, 2) To UBound(EcartsProduits, 2)
Debug.Print EcartsProduits(0, IndexMatrice) _
& " " & EcartsProduits(1, IndexMatrice) _
& " " & EcartsProduits(2, IndexMatrice) _
& ", S : " & EcartsProduits(3, IndexMatrice) _
& ", S+1 : " & EcartsProduits(4, IndexMatrice)
Next IndexMatrice
End If
Set Pvt = Nothing
Set Pvc = Nothing
Set AireSemaine = Nothing
Set AireTableau = Nothing
Set AireProduit = Nothing
Set ShEnsemble = Nothing
End Sub |
Partager