我制作了一个宏,根据我在列表框(Lbox1)中的选择,在多个数据系列上创建动态图表。每当我在列表框中进行任何选择时,宏都会运行。但是,每当我最初删除所有数据系列以创建新图表时,它都会抛出错误。 我是VBA的新手。请帮我解决这个问题。
Public Sub listbox_selection()
Dim i As Integer
Dim temp As String
Dim k As Integer
Dim s As SeriesCollection
k = Sheets("Plan1").ChartObjects(1).Chart.SeriesCollection.count
##This part giving error
For i = 1 To k
Sheets("Plan1").ChartObjects(1).Chart.SeriesCollection(i).Delete
Next
####
Sheets("Plan1").ListBoxes("LBox1").Select
For i = 1 To Sheets("Plan1").Shapes("LBox1").ControlFormat.ListCount
If Sheets("Plan1").ListBoxes("LBox1").Selected(i) = True Then
Call Listit(X:=i)
End If
Next
End Sub
Public Sub Listit(ByVal X As Integer)
X = X + 3
With Sheets("Plan1").ChartObjects(1).Chart
With .SeriesCollection.NewSeries
.XValues = Range("Q2:U2")
.Values = Range("Q" & X & ":U" & X & "")
.Name = Range("P" & X).Value
End With
End With
End Sub
答案 0 :(得分:11)
试试这个,
Sub temp()
ActiveSheet.ChartObjects("Chart 1").Activate
For Each s In ActiveChart.SeriesCollection
s.Delete
Next s
End Sub
答案 1 :(得分:6)
您可以使用Sheets("Plan1").ChartObjects(1).Chart.ChartArea.ClearContents
答案 2 :(得分:1)
Excel 2010
ActiveChart.ChartArea.ClearContents