Excel - Power Query - Power Pivot - VBA

Fonctionnalités d'Excel

Données > Validation

Crée une liste de valeur de validation de type Liste de valeur :

With Selection.Validation
  .Delete
  .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= xlBetween, Formula1:="A;B;C"
  .IgnoreBlank = True
  .InCellDropdown = True
  .InputTitle = ""
  .ErrorTitle = ""
  .InputMessage = ""
  .ErrorMessage = ""
  .ShowInput = True
  .ShowError = True
End With

Protéger la feuille

Protège la feuille active :

ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= True

Empêche la sélection des cellules verrouillées :

ActiveSheet.EnableSelection = xlUnlockedCells

Déprotège la feuille active :

ActiveSheet.Unprotect

Protéger le classeur

Protège le classeur :

ActiveWorkbook.Protect Structure:=True, Windows:=False

Déprotège le classeur actif :

ActiveWorkbook.Unprotect

Trier une plage

ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Clear 
ActiveWorkbook.Worksheets("Feuil1").Sort.SortFields.Add Key:=Range("A2:A4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Feuil1").Sort
  .SetRange Range("A1:A4")
  .Header = xlYes
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With

Filtrer une plage

Filtrer une plage en n’affichant que les lignes dont la valeur est « Zone 1 »

ActiveSheet.Range("$A$1:$A$4").AutoFilter Field:=1, Criteria1:="Zone 1"

Tableau Croisé Dynamique

Création

Dim objPivotTable As PivotTable
Dim objPivotCache As PivotCache
Dim rngPlage as Range

On crée une variable Range dans laquelle on place la référence à la plage à mettre en TCD :

'Remplacer Selection par la référence à la plage à mettre en TCD
  Set rngRange = Selection

On commence par créer un cache :

'Remplacer Feuil1!A1:B5 par la plage source du TCD
  Set objPivotCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Range("Feuil1!A1:B5"), Version:=xlPivotTableVersion14)

Puis on crée le TCD :

'Remplacer Feuil2 par la feuille de destination (laissez A3)
'Remplacer TCD par un nom de TCd que vous inventez
Set objPivotTable = objPivotCache.CreatePivotTable(TableDestination:=Range("Feuil2!A3"), TableName:="TCD", DefaultVersion:=xlPivotTableVersion14)

Manipulations

Ajouter un champ en ligne

With objPivotTable.PivotFields("Produit")
  .Orientation = xlRowField
  .Position = 1
End With

Ajouter un champ en colonne

With objPivotTable.PivotFields("Pays")
  .Orientation = xlColumnField
  .Position = 1
End With

Ajouter un champ de données

With objPivotTable
  .AddDataField objPivotTable.PivotFields("Qte"), Caption:="Total Qte", Function:=xlSum
End With

Afficher les valeurs en % de la colonne

With objPivotTable.PivotFields("Pourcentage")
  .Calculation = xlPercentOfColumn   .NumberFormat = "0,00%"
End With

Ajouter un champ de page (filtre)

With objPivotTable.PivotFields("Année")
  .Orientation = xlPageField
  .Position = 1
End With

Filtrer le TCD sur une valeur. On affiche ici la valeur 2013 du champ Année :

objPivotTable.PivotFields("Année").ClearAllFilters
objPivotTable.PivotFields("Année").CurrentPage = "2013"

Masquer une valeur d’un champ de ligne ou de colonne. On veut masquer la valeur "Produit 1" du champ Produit

With objPivotTable.PivotFields("Produit")
  .PivotItems("Produit 1").Visible = False
End With