Bonjour !
A partir d'Access, j'ouvre un classeur excel dans lequel j'affiche un tableau , puis un graphique sur une deuxième feuille.
Malheureusement, mon code fonctionne très bien la première fois que je l'éxecute, mais plante la deuxième fois:
Excel s'ouvre, fait le tableau,mais refuse de créer le graphique.
J'ai le message d'erreur :
Erreur d'execution '-2147417851 (80010105)
La méthode 'SetSourceData' de l'objet' _Chart' a échoué.
Voici mon code :
Quelqu'un a t'il deja rencontré ce problème ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Set Db = CurrentDb 'automation pour piloter excel Dim xlApp As Excel.Application Dim xlSheet As Excel.Worksheet Dim xlBook As Excel.Workbook 'Initialisations Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Add xlApp.Visible = True 'Ajouter une feuille Set xlSheet = xlBook.Worksheets.Add xlSheet.Name = "Fonction" Set xlSheet = xlBook.ActiveSheet 'Créer un nouveau classeur ' Set wbk = .Workbooks.Add '************************************************************************************************************************** 'tableau1 : quelle est la fonction des personnes formées '************************************************************************************************************************** 'compter le nombre de personnes formées par fonction Dim Rq_Tab As String Rq_Tab = " SELECT count(T_Contact.Num_Contact) AS NbPersForm, T_Contact.Fonction_Contact FROM T_Contact" Rq_Tab = Rq_Tab & " WHERE (T_Contact.Date1Formation_Contact Is Not Null Or T_Contact.Date2Formation_Contact Is Not Null)" Rq_Tab = Rq_Tab & " GROUP BY T_Contact.Fonction_Contact ORDER BY T_Contact.Fonction_Contact DESC;" Dim Rst_Tab As Recordset Set Rst_Tab = Db.OpenRecordset(Rq_Tab) i = 5 While Not Rst_Tab.EOF xlSheet.Cells(i, 2) = Rst_Tab![Fonction_Contact] xlSheet.Cells(i, 3) = Rst_Tab![NbPersForm] i = i + 1 Rst_Tab.MoveNext Wend xlSheet.Cells(i - 1, 2) = "Non renseigné" Rst_Tab.Close xlSheet.Columns(2).AutoFit xlSheet.Columns(3).AutoFit o = "B5:C" & i - 1 '****************************************************************************************************************************** 'graphique tableau 1 '****************************************************************************************************************************** Dim objChart As Chart, objRange As Range Set objRange = Worksheets("Fonction").Range("(" & o & ")") Set objChart = xlBook.Charts.Add objChart.Name = "Graph-Fonction" objChart.ChartType = xlColumnClustered objChart.SetSourceData objRange, xlColumns With ActiveChart .HasTitle = True .ChartTitle.Characters.Text = "Quelle est la fonction des personnes formées par PBE+ ?" .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Fonction" .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Nombre de personnes" End With objChart.SeriesCollection(1).Name = "Nombre de personnes formées" objChart.SeriesCollection(1).ApplyDataLabels xlDataLabelsShowValue, False
Merci d'avance pour vos conseils.
Partager