Skip to content
Excel - Power Query - Power Pivot - VBA
  • Pages
    • Excel - Power Query - Power Pivot - VBA
    • Raccourcis clavier
    • Généralités
      • Quel stage Excel choisir ?
        • Parcours de formation Excel
        • Une démarche BI avec Excel ?
        • Excel, VBA ou Power Pivot / Query / BI ?
      • Installation des outils
    • Manipulations de bases
      • Gestion des feuilles
      • Formater les cellules
      • Lignes et colonnes
      • Trier et filtrer
    • Temps 1 | Organiser ses données en tableau
      • Concevoir un tableau
      • Tableau dans Excel
    • Temps 2 | Saisir et importer
      • Valider ses données
      • Protéger les données
      • Mise en forme conditionnelle
      • Types de données (tables recommandées)
    • Temps 3 | Calculer
      • Calculs
      • Fonctions de texte
      • Fonctions de date
      • Fonctions logiques
      • Fonctions de synthèse
      • Fonction de recherche
      • Fonctions d'information
      • Formules matricielles ou Tableaux dynamiques [365]
      • Nouvelles fonctions [365]
    • Temps 4 | Présenter et synthétiser
      • Mise en forme avec les styles
      • Mise en page et impression
      • Tableaux croisées dynamiques
        • TCD multi tables
      • Graphiques croisés dynamiques
    • A à Z : créer un classeur d'analyse [Exemple 002]
    • Power Query
    • Power Pivot
      • Présentation
      • Mise en pratique
      • Exercice 01
    • VBA
      • Visual Basic
        • Visual Basic Editor
        • Variables
        • Décisions
        • Boucles
        • Fonctions
          • Quelques fonctions intrinsèques
        • Organisation du code
        • Procédures
      • VB Excel
        • Objets Excel
        • Classeurs
        • Feuilles
        • Plages et cellules
        • Tableaux
        • icon picker
          Fonctionnalités d'Excel
        • SQL & VBA
        • Boucle des fichiers d'un dossier
        • Manipuler d'autres logiciels depuis Excel
      • Créer une application Excel
      • API REST avec JSON
      • Exercice Signalements
    • Exercices
      • Excel 1 |
    • Office Script
    • Office Scripts pour Excel : Comparaison avec VBA
    • Échanges stagiaires

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
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.