在VBA中的多个Excel工作表上构build2轴图表

在Excel中使用VBA我试图遍历所有的工作表,并绘制每张工作表的相同位置的数据。 这将使用10列中的4列build立一个双轴图。 下面的代码成功地循环和绘制,但是只有第一张和最后一张被正确构build。 其他表格绘制所有10列,而不是只是所需的设置。

Sub plotLoop() ' Declare Current as a worksheet object variable. Dim Current As Worksheet ' Loop through all of the worksheets in the active workbook. For Each Current In Worksheets Current.Select Current.Shapes.AddChart.Select ActiveChart.ChartType = xlXYScatter ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(1).XValues = Sheets(Current.Name).Range("$A$17:$A$350") ActiveChart.SeriesCollection(1).Values = Sheets(Current.Name).Range("$B$17:$B$350") ActiveChart.SeriesCollection.NewSeries ActiveChart.SeriesCollection(2).XValues = Sheets(Current.Name).Range("$I$17:$I$350") ActiveChart.SeriesCollection(2).Values = Sheets(Current.Name).Range("$J$17:$J$350") ActiveChart.SeriesCollection(1).Select ActiveChart.SeriesCollection(1).AxisGroup = 2 ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis) Selection.Caption = "Time(S)" ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated) Selection.Caption = "Units(u)" ActiveChart.SetElement (msoElementLegendNone) ActiveChart.SetElement (msoElementChartTitleAboveChart) Selection.Caption = "Rolling Charts" Next End Sub 

我没有错误地运行你的代码。

应该是一个“select”的问题,为了赶上它,你必须在debugging模式下运行你的代码,逐步通过每个代码行,并检查实际的variables值。

我在此附上一个替代编码风格的你的子,不使用select,这应该让你更多的把握你实际处理的对象,并希望导致任何错误

 Sub plotLoop2() ' Declare Current as a worksheet object variable. Dim Current As Worksheet Dim X1Range As Range, Y1Range As Range, X2Range As Range, Y2Range As Range Dim series1 As Series, series2 As Series Const X1Address As String = "$A$17:$A$350" Const Y1Address As String = "$B$17:$B$350" Const X2Address As String = "$I$17:$I$350" Const Y2Address As String = "$J$17:$J$350" ' should those not be constants, then comment the four lines above and uncomment the following five lines 'Dim X1Address As String, Y1Address As String, X2Address As String, Y2Address As String 'X1Address = "$A$17:$A$350" 'Y1Address = "$B$17:$B$350" 'X2Address = "$I$17:$I$350" 'Y2Address = "$J$17:$J$350" ' Loop through all of the worksheets in the active workbook. For Each Current In Worksheets With Current 'set current sheet XY ranges Set X1Range = .Range(X1Address) Set Y1Range = .Range(Y1Address) Set X2Range = .Range(X2Address) Set Y2Range = .Range(Y2Address) .Shapes.AddChart With .Shapes(.Shapes.Count) Set series1 = addOneSeries(.Chart, X1Range, Y1Range) Set series2 = addOneSeries(.Chart, X2Range, Y2Range) With .Chart .ChartType = xlXYScatter .SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis) .Axes(xlCategory, xlPrimary).AxisTitle.Caption = "Time(S)" .SetElement (msoElementPrimaryValueAxisTitleRotated) .Axes(xlValue, xlPrimary).AxisTitle.Caption = "Units(u)" .HasLegend = False .SetElement (msoElementChartTitleAboveChart) .ChartTitle.Caption = "Rolling Charts" End With series1.AxisGroup = 2 End With End With Next End Sub Function addOneSeries(myChart As Chart, XRange As Range, YRange As Range) As Series With myChart Set addOneSeries = .SeriesCollection.NewSeries With addOneSeries .XValues = XRange .Values = YRange End With End With End Function