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 WithProtéger la feuille
Protège la feuille active :
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= TrueEmpêche la sélection des cellules verrouillées :
ActiveSheet.EnableSelection = xlUnlockedCellsDéprotège la feuille active :
ActiveSheet.UnprotectProtéger le classeur
Protège le classeur :
ActiveWorkbook.Protect Structure:=True, Windows:=FalseDéprotège le classeur actif :
ActiveWorkbook.UnprotectTrier 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 WithFiltrer 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 RangeOn 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 = SelectionOn 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 WithAjouter un champ en colonne
With objPivotTable.PivotFields("Pays")
.Orientation = xlColumnField
.Position = 1
End WithAjouter un champ de données
With objPivotTable
.AddDataField objPivotTable.PivotFields("Qte"), Caption:="Total Qte", Function:=xlSum
End WithAfficher les valeurs en % de la colonne
With objPivotTable.PivotFields("Pourcentage")
.Calculation = xlPercentOfColumn .NumberFormat = "0,00%"
End WithAjouter un champ de page (filtre)
With objPivotTable.PivotFields("Année")
.Orientation = xlPageField
.Position = 1
End WithFiltrer 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