Pb insertion graphique dans un userform

Malka

XLDnaute Occasionnel
Salut à tous,

J'ai des problemes pour inserer un graphique dans un userform.
Pour creer mon fameux graphique, j'utilise le code suivant :

Sub Graphique()
y = ActiveCell.Row

Union(Range("D2:O2"), Range("D" & y, "O" & y)).Select

Charts.Add

ActiveChart.ChartType = xlLineMarkers

With ActiveChart.SeriesCollection.NewSeries
.Name = "2010"
.Values = ThisWorkbook.Sheets("ORYS").Range("S" & y, "AD" & y).Value
.XValues = ThisWorkbook.Sheets("ORYS").Range("D2:O2").Value
End With

ActiveChart.Location Where:=xlLocationAsObject, Name:="ORYS"
End Sub

Je voudrais que ce graphique soit créé directement dans le userform apres avoir clické sur un bouton... :rolleyes:
Je n'y arrive pas malheureusement :confused:

Si vous avez la reponse je suis preneuse. :eek:
Merci

Malka
 

PMO2

XLDnaute Accro
Re : Pb insertion graphique dans un userform

Bonjour,

Une piste avec la démarche suivante

1) Créer un UserForm1 et copiez le code suivant dans sa fenêtre de code

Code:
Dim Chrt As clsEventChartSpace
Dim CS As Control
Dim C As Object
 
Private Sub UserForm_Initialize()
If Renseigne Then
  With Me
    .Height = 360
    .Width = 625
  End With
  Set CS = Me.Controls.Add("OWC11.ChartSpace.11")
  With CS
    .Top = Me.Top + 2
    .Left = Me.Left + 2
    .Height = Me.Height - 47
    .Width = Me.Width - 8
    Set C = .Constants
    Set CHT = .Charts.Add
  End With
  Set LB = Me.Controls.Add("forms.Label.1")
  With LB
    .Top = CS.Height + 5
    .Left = 10
    .Height = 10
    .Width = 180
  End With
  With CHT
    .Type = C.chChartTypeLineMarkers
    .SeriesCollection.Add
    .SetData C.chDimCategories, C.chDataLiteral, DATA1
    .SeriesCollection(0).SetData C.chDimValues, C.chDataLiteral, DATA2
    .SeriesCollection(0).Interior.Color = 240 'RGB(0, 0, 240)
  End With
  
  '--- Pour les évènements ---
  Set Chrt = New clsEventChartSpace
  Set Chrt.myChartSpace = CS
  '---------------------------
Else
  Me.Caption = "Ne pas lancer directement le UserForm à partir de lui-même"
End If
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Set LB = Nothing
Set C = Nothing
Set CHT = Nothing
Set CS = Nothing
Set Chrt = Nothing
End Sub


2) copiez le code suivant dans un module standard

Code:
'####################################################################################
'### Nécessite la référence Library OWC11
'### C:\Program Files\Fichiers communs\Microsoft Shared\Web Components\11\OWC11.DLL
'### Microsoft Office Web Components 11.0
'####################################################################################

Const FEUILLE As String = "ORYS"

Public Ligne&
Public DATA1
Public DATA2
Public Renseigne As Boolean
Public CHT As Object 'OWC11.ChChart
Public LB As MSForms.Label

Sub LaunchUSF()
Dim S As Worksheet
Dim T1()
Dim T2()
Dim i&
Set S = Sheets(FEUILLE)
Ligne& = ActiveCell.Row
If Ligne& < 3 Or Ligne& > 6 Then
  MsgBox "Veuillez sélectionner une cellule dans la plage concernée"
  Exit Sub
End If
For i& = 1 To 12
  ReDim Preserve T1(1 To i&)
  T1(i&) = S.Cells(2, i& + 3)
  ReDim Preserve T2(1 To i&)
  T2(i&) = S.Cells(Ligne&, i& + 3)
Next i&
Set S = Nothing
Renseigne = True
DATA1 = T1
DATA2 = T2
UserForm1.Show
End Sub


3) Créez un module de classe et renommez le clsEventChartSpace puis, dans sa fenêtre de code, copiez le code suivant

Code:
Public WithEvents myChartSpace As OWC11.ChartSpace

Private Sub myChartSpace_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
Randomize
CHT.SeriesCollection(0).Interior.Color = Int(((256 ^ 3) * Rnd) + 1)
End Sub

Private Sub myChartSpace_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
With LB
  .Caption = "Coordonnées graphique X: " & x & "  Y: " & y
End With
End Sub


4) Dans le VBE faites menu Outils/Références et parcourez jusqu'à trouver OWC11.DLL
Chez moi le chemin est C:\Program Files\Fichiers communs\Microsoft Shared\Web Components\11\OWC11.DLL

Reportez vous à la pièce jointe pour voir la feuille "ORYS"

Cordialement.

PMO
Patrick Morange
 

Discussions similaires

Réponses
1
Affichages
196
Réponses
0
Affichages
176

Statistiques des forums

Discussions
312 492
Messages
2 088 924
Membres
103 983
dernier inscrit
AlbertCouillard