vba : creation tcd et forcer le nom des champs

miguelangelo

XLDnaute Occasionnel
Bonsoir à tous,

je vais essayer de vous expliquer mon problème.
j'essaye de développer un outil qui tourne aussi bien sous xls 2000 que 2007.
Mon souci est que j'ai une macro qui génère un tcd.

sous 2000, le nom d'une colonne dans mon tcd va s'appeler "somme...."
sous 2003, le nom de la meme colonne du tcd va s'appeler "somme de ..."

après création du tcd je demande à renommer le nom de la colonne et ... ca plante !!!
mon probleme en zone rouge... merci pour votre aide.



Sub cca_generation()
If Sheets("Donnees").Range("a2").Value = "" Then
MsgBox ("aucune donnée en traitement")
GoTo fin
End If
Dim taux As String
Application.ScreenUpdating = False
For Each c In Range("mois")
If c = Range("mois_en_cours") Then taux = c.Offset(0, 1)
Next c

Application.Run ("propre")

If taux = 0 Then
MsgBox ("Pas de CCA ce mois ci")
GoTo fin
End If

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"totalité").CreatePivotTable TableDestination:=Range("e21"), _
TableName:="CCA à intégrer"
' On Error GoTo erreur
ActiveSheet.PivotTables("CCA à intégrer").SmallGrid = False
ActiveSheet.PivotTables("CCA à intégrer").HasAutoFormat = False
ActiveSheet.PivotTables("CCA à intégrer").AddFields RowFields:= _
Array("CIA", "région"), ColumnFields:="Données", PageFields:="Intégration"
ActiveSheet.PivotTables("CCA à intégrer").PivotFields("CIA"). _
Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
False, False)
Range("F26").Select

If taux = "0,67" Then
ActiveSheet.PivotTables("CCA à intégrer").CalculatedFields.Add _
"loyer", "= ('Loyer à 19,6'+ 'loyer à 5,5'+ 'loyer à 0')/3*2"
ActiveSheet.PivotTables("CCA à intégrer").CalculatedFields.Add _
"charges", "= ('Charges avec 19,6'+ 'charges à 5,5'+ 'charges à 0')/3*2"
ActiveSheet.PivotTables("CCA à intégrer").CalculatedFields.Add _
"taxes", "=('taxes a 19,6'+ 'taxes à 5,5'+ 'taxes à 0')/3*2"
GoTo suite
End If

If taux = "0,33" Then

ActiveSheet.PivotTables("CCA à intégrer").CalculatedFields.Add _
"loyer", "= ('Loyer à 19,6'+ 'loyer à 5,5'+ 'loyer à 0')/3"
ActiveSheet.PivotTables("CCA à intégrer").CalculatedFields.Add _
"charges", "= ('Charges avec 19,6'+ 'charges à 5,5'+ 'charges à 0')/3"
ActiveSheet.PivotTables("CCA à intégrer").CalculatedFields.Add _
"taxes", "=('taxes a 19,6'+ 'taxes à 5,5'+ 'taxes à 0')/3"
GoTo suite
End If

suite:

ActiveSheet.PivotTables("CCA à intégrer").PivotFields("loyer"). _
Orientation = xlDataField
ActiveSheet.PivotTables("CCA à intégrer").PivotFields("charges"). _
Orientation = xlDataField
ActiveSheet.PivotTables("CCA à intégrer").PivotFields("taxes"). _
Orientation = xlDataField
Range("F21").Select
With ActiveSheet.PivotTables("CCA à intégrer").PivotFields("Données" _
)
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("CCA à intégrer").PivotFields("Intégration") _
.CurrentPage = Range("trimestre") & "-" & Range("annee")
Range("F23:i400").Select
Selection.NumberFormat = "0"
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
ActiveSheet.PivotTables("CCA à intégrer").PivotFields("Données"). _
PivotItems("Somme loyer").Caption = " loyer"
ActiveSheet.PivotTables("CCA à intégrer").PivotFields("Données"). _
PivotItems("Somme charges").Caption = " charges"
ActiveSheet.PivotTables("CCA à intégrer").PivotFields("Données"). _
PivotItems("Somme taxes").Caption = " taxes"

Range("F22:i22").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
ActiveWindow.FreezePanes = False
Range("A23").Select
ActiveWindow.FreezePanes = True
Application.ScreenUpdating = True
Rows("17:3000").Select
Selection.Interior.ColorIndex = 15
Range("E19,E21:i22").Select
Selection.Interior.ColorIndex = 49
Selection.Font.ColorIndex = 2
Range("a1").Select
GoTo fin

erreur:
MsgBox ("vérifier la présence de données sur la période voulue")
Application.Run "propre"


fin:
Application.ScreenUpdating = True
End Sub

:confused:
 

Pierrot93

XLDnaute Barbatruc
Re : vba : creation tcd et forcer le nom des champs

Bonjour,

regarde ceci, on ne devrait pas être loin de la vérité.... non testé, un petit fichier aurait été un plus...
Code:
Option Explicit
Sub Test()
Dim pf As PivotField, pi As PivotItem
For Each pf In ActiveSheet.PivotTables("CCA à intégrer").PivotFields("Données")
    For Each pi In pf.PivotItems
        pi.Caption = " " & pi.SourceName
    Next pi
Next pf
End Sub

bonne soirée
@+
 

Discussions similaires

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 206
Messages
2 086 219
Membres
103 158
dernier inscrit
laufin