Excel - Power Query - Power Pivot - VBA
Share
Explore
VB Excel

icon picker
Tableaux

1 | Déclaration d'un Tableau

Dim tblvente As ListObject

2 | Affectation d'un Tableau

Le Tableau existe déjà

Vous devez savoir dans quelle feuille se trouve le Tableau.
On pointe vers le Tableau par son numéro :
Set tblVente = VariableFeuille.ListObjects(1)
Ou avec son nom
Set tblVente = VariableFeuille.ListObjects("tblvente")

Le Tableau doit être créé

Crée un Tableau incluant les cellules autour de la cellule A1 de la feuille active:
Set tblVente = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes)

3 | Manipulation d'un Tableau

Tableau

Nommer

VariableTableau.Name = "tblVente"

Convertir en plage

VariableTableau.Unlist

Trier un Tableau

VariableTableau.Sort.SortFields.Clear
VariableTableau.Sort.SortFields.Add Key:=Range("tblVente[[#Headers],[#Data],[Zone]]"), SortOn:= xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With VariableTableau.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Parcourir des lignes

Dim VariableLigne As ListRow
For Each VariableLigne In VariableTableau.ListRows
Debug.Print VariableLigne.Range.Columns(1)
Next

Afficher la ligne Total

VariableTableau.ShowTotals = True
VariableTableau.ListColumns("TTC").TotalsCalculation = xlTotalsCalculationSum

Filtrer un Tableau

Enlève le filtre :
VariableTableau.Range.AutoFilter Field:=3
Place le filtre :
VariableTableau.Range.AutoFilter Field:=3, Criteria1:="=1", Operator:=xlAnd

Lignes

Ajouter une ligne

Dim NewLine As ListRow
Set NewLine = tblVente.ListRows.Add

Écrire une ligne

Le Tableau comporte les colonnes Zone et CA. On souhaite ajouter une ligne :
Dim NewLine As ListRow
NewLine.Range.Cells(, tblVente.ListColumns("Zone").Index).Value = "Zone1"
NewLine.Range.Cells(, tblVente.ListColumns("CA").Index).Value = "50"
On peut aussi désigner une cellule de la nouvelle ligne par son numéro :
NewLine.Range.Colums(1).Value = "Zone1"
NewLine.Range.Colums(2).Value = "50"

Supprimer une ligne

Supprimer la ligne 5 :
tblVente.ListRows(5).Delete

Effacer toutes les lignes

Supprimer toutes les lignes de données :
If Not tblVente.DataBodyRange Is Nothing Then
tblVente.DataBodyRange.Delete
End if
Notez qu’il reste toujours une ligne vide mais les formules ne sont pas supprimées : quand vous collerez ou saisirez des données dans le Tableau, les formules seront propagées dans les colonnes calculées.

Colonnes

Créer une colonne

Dim NewCol As ListColumn
Set NewCol = tblVente.ListColumns.Add
NewCol.Name="TTC"
NewCol.DataBodyRange.Cells(1) = "=[@CA]*1.196"

Supprimer une colonne

Supprimer la colonne "CA"
tblVente.ListColumns("CA").Delete
Supprimer la colonne 5
tblVente.ListColumns(5).Delete

Formate une colonne

Appliquer un format Date
VariableTableau.ListColumns("CA").DataBodyRange.NumberFormat = "m/d/yyyy"
Appliquer un format Pourcentage
VariableTableau.ListColumns("CA").DataBodyRange.NumberFormat = "0.00%"
Appliquer un format Nombre avec séparateur de miliers
VariableTableau.ListColumns("CA").DataBodyRange.NumberFormat = "#,##0.00"

Résumé

image.png
image.png
image.png
Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.