1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| Sub ManageFields(strSheetName As String)
'permet de gérer chaque champ de maniere indépendante
Dim rst As New ADODB.Recordset
Dim cnn As New ADODB.Connection
Dim intOrientation As Integer
Dim intFunction As Integer
Dim strField As String
Dim strCol As String
Dim pvtField As PivotField
Dim newChart As Chart
Dim boIsChart As Boolean
'connection sur le fichier excel actif
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
'gestion des graphs
'[GfxSheet] est une plage nommée
rst.Open "select * from [GfxSheet] Where [Based on data worksheet]='" & strSheetName & "'", cnn
If rst.EOF Then
Set newChart = Nothing
boIsChart = False
Else
'creation du graphique croisé dynamique
Set newChart = Charts.Add
newChart.Location Where:=xlLocationAsNewSheet
newChart.Name = strSheetName & "_Graph"
newChart.ChartType = GetChartType(rst.Fields("Graph Type"))
boIsChart = True
'mise en forme du graph
Call FormatGraph(newChart)
End If
rst.Close
'parcours l'ensemble des champ a afficher en champ de page / ligne / colonne / donnée
'[ColumnGfx] est une plage nommée
rst.Open "select * from [ColumnGfx] Where WorkSheet='" & strSheetName & "' ORDER BY 6 DESC, 3 ASC", cnn, adOpenStatic, adLockOptimistic
Do While Not rst.EOF
strField = rst.Fields("Column")
intOrientation = GetOrientation(rst.Fields("Orientation"))
intFunction = GetFunction(NZ(rst.Fields("Calculation type"), ""))
'si c est un champ de donnée
If intOrientation = xlDataField Then
Set pvtField = Worksheets(strSheetName).PivotTables(1).PivotFields(strField)
With Worksheets(strSheetName).PivotTables(1)
.AddDataField pvtField, NZ(rst.Fields("Calculation type"), "") & " " & strField, intFunction
End With
Else
'si c est un champ de page / colonne / ligne
With Worksheets(strSheetName).PivotTables(1).PivotFields(strField)
.Orientation = intOrientation
.Position = rst.Fields("Position")
'si c'est un champ de colonne
If intOrientation = xlColumnField And boIsChart Then
strCol = NZ(rst.Fields("Column Value"), "")
Do While strCol <> "" And strCol = NZ(rst.Fields("Column Value"), "") And Not rst.EOF
newChart.SeriesCollection(strCol).ChartType = GetChartType(rst.Fields("Graph Type"))
rst.MoveNext
If rst.EOF Then Exit Do
strCol = NZ(rst.Fields("Column Value"), "")
Loop
rst.MoveLast
End If
End With
End If
'affiche une valeur par défaut
If Not IsNull(rst.Fields("Selected")) Then _
Worksheets(strSheetName).PivotTables(1).PivotFields(strField).CurrentPage = "P1"
rst.MoveNext
Loop
'enregistre les formats des différentes series
If boIsChart Then
Application.AddChartAutoFormat Chart:=newChart, Name:=newChart.Name, Description:=""
newChart.ApplyCustomType ChartType:=xlUserDefined, TypeName:=newChart.Name
Call AddEventToChart(newChart.Name)
End If
rst.Close
Set rst = Nothing
Set newChart = Nothing
End Sub |
Partager