Je cherche à faire par macro ce que l'on fait simplement manuellement (par glisser-déposer), à savoir passer les champs de données en colonne.
Dans l'onglet "test" du fichier joint, vous trouverez 2 tables de pilote :
- la première est générée par la macro (clic sur le bouton "Créer pilote" de l'onglet "Base de données")
- la seconde est le résultat attendu.
Ci dessous le code pour créer la table de pilote :
Code : Tout sélectionner
Sub tablePilote(oShTable as Object, oCellTable as Object, oTableNom as String, oShSource as Object)
Dim oTables as Object, oTableDescr as Object
Dim derCol as Long, derLg as Long
Dim i as Integer
derCol = LastElement("Colonne",oShSource) 'La fonction LastElement permet de récupérer la dernière ligne ou colonne de la feuille indiquée.
derLg = LastElement("Ligne",oShSource)
oTables = oShTable.getDataPilotTables()
oTableDescr = oTables.createDataPilotDescriptor()
oTableDescr.setSourceRange(oShSource.getCellRangeByPosition(0,4,derCol,derLg).RangeAddress)
For i = 0 to 3
With oTableDescr.getDataPilotFields().getByIndex(i)
If i = 0 Then
.setPropertyValue("Orientation", com.sun.star.sheet.DataPilotFieldOrientation.COLUMN)
Else
.setPropertyValue("Orientation", com.sun.star.sheet.DataPilotFieldOrientation.ROW)
End If
.setPropertyValue("Function", com.sun.star.sheet.GeneralFunction.NONE)
End With
Next i
For i = 11 to 14
With oTableDescr.getDataPilotFields().getByIndex(i)
If i = 14 Then
.setPropertyValue("Orientation", com.sun.star.sheet.DataPilotFieldOrientation.PAGE)
.setPropertyValue("Function", com.sun.star.sheet.GeneralFunction.NONE)
Else
.setPropertyValue("Orientation", com.sun.star.sheet.DataPilotFieldOrientation.DATA)
.setPropertyValue("Function", com.sun.star.sheet.GeneralFunction.SUM)
End If
End With
Next i
If oTables.hasByName(oTableNom) then oTables.removeByName(oTableNom)
oTables.insertNewByName(oTableNom, oCellTable.CellAddress, oTableDescr)
End Sub
Merci de votre aide !