Bien le bonjour,
Je suis débutante en Visual Basic et je suis coincée sur un problème :
J'ai créée une application vb fonctionnant avec une base de données Access. A l'aide
d'un bouton, une feuille Excel se créée, des données y sont inscrites automatiquement
et un graphique se crée. Si je clique une fois sur le bouton, ça marche bien.
Cependant si j'essai de cliquer une deuxième fois sur le même bouton, le
graphique ne veut plus se recréer dans la deuxième feuille Excel. (mais les
données oui).
A mon avis c'est Chars.Add qui considère qu'il est déjà crée.
voici mon code :
[...déclaration des variables...]
public nbXLS As Integer
[...]
[... dans le frame_load...]
nbXLS = 0
[...]
Private Sub HC_Bouton1_Click()
Dim graphique As ChartObject
Dim i As Integer
Set MonXL = New Excel.Application
nbXLS = nbXLS + 1
MonXL.Caption = "Anthracologie Excel - Graphique général N°" & nbXLS
'on rend visible l'application
MonXL.Visible = True
'création d'un nouveau classeur
MonXL.Workbooks.Add
'On renomme la feuille 1 en graph
MonXL.ActiveWorkbook.Worksheets("Feuil1").Name = "Graph"
'J'écris dans la feuille "Graph" les informations de base
With MonXL.ActiveWorkbook.Worksheets(1)
.Range("A1:C3").HorizontalAlignment = 3
.Range("A1:C1").ColumnWidth = 20
.Range("A1") = "Echantillon"
.Range("A1").BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
.Range("A1").Interior.Color = &H707070
.Range("B1") = DBG_echantillon.Columns(0)
.Range("B1").BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
.Range("B1").Interior.Color = &HC0C0C0
.Range("A3") = "Espèces"
.Range("B3") = "Effectif"
.Range("C3") = "Masse"
.Range("B3").BorderAround xlContinuous, xlThin, xlColorIndexAutomatic
.Range("A3:C3").BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
.Range("A3:C3").Interior.Color = &HC0C0C0
'pour les nom d'éspèces
MSF_recap.col = 0
For i = 1 To MSF_recap.Rows - 1
MSF_recap.Row = i
.Range("A" & i + 3) = MSF_recap.Text
Next i
'pour les effectifs
MSF_recap.col = 1
For i = 1 To MSF_recap.Rows - 1
MSF_recap.Row = i
.Range("B" & i + 3) = MSF_recap.Text
Next i
'pour les masses
MSF_recap.col = 18
For i = 1 To MSF_recap.Rows - 1
MSF_recap.Row = i
.Range("C" & i + 3) = MSF_recap.Text
.Range("A" & i + 3 & ":C" & i + 3).BorderAround
xlContinuous, xlThin, xlColorIndexAutomatic
Next i
'apparence
.Range("A4:C" & i + 2).HorizontalAlignment = 3
.Range("B4:B" & i + 2).BorderAround xlContinuous, xlThin, xlColorIndexAutomatic
.Range("A4:C" & i + 2).BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
'pour le graphique
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Graph").Range("A4:C10"), PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Graph"
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).ChartType = xlXYScatter
ActiveChart.SeriesCollection(2).Select
With Selection.Border
.Weight = xlHairline
.LineStyle = xlNone
End With
With Selection
.MarkerBackgroundColorIndex = 1
.MarkerForegroundColorIndex = 1
.MarkerStyle = xlCircle
.Smooth = False
.MarkerSize = 5
.Shadow = False
End With
ActiveChart.SeriesCollection(2).AxisGroup = 2
ActiveChart.ChartArea.Select
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Graphique"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Effectifs"
.Axes(xlValue, xlSecondary).HasTitle = True
.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "Masses"
End With
ActiveChart.HasLegend = False
End With
Exit Sub
End Sub
Merci de m'aider

svp