如何解决“编译错误:下一个没有for”在VBA中的错误?

所以我对VBA编程或一般的编程都是很新的。 我正在收到“编译错误:下一步没有”。 我相信我为了三分而给三分,但我还是不知道。 以下是我正在处理的代码….

Sub width2()'自动分配系列宽度

For Series = 1 To 24 'chart series, 144 combinations For i = 0 To 1 For j = 0 To 11 ActiveSheet.ChartObjects("Chart 4").Activate ActiveChart.FullSeriesCollection(Series).Select With Selection.Format.Line .Visible = msoTrue .Weight = Sheet5.Range("Q9").Offset((9 * i) + 6, (3 * j)).Value Next j Next i Next Series End Sub 

不知道错误在哪里。 任何帮助非常感谢谢谢。

既然你没有分享你的工作表数据结构,这不是完整的答案。

代码运行时无需使用ActivateActiveChartSelect 。 相反,它引用“Chart 6” ChartObject ,然后使用以下With语句修改SeriesCollectionLine.VisibleLine.WeightWith MyCht.Chart.SeriesCollection(12 * i + j + 1)

注意 :如果@Piyush Verma决定共享他的数据结构,也是Sheet5.Range("Q9").Offset((9 * i) + 6, (3 * j)).Value的math部分Sheet5.Range("Q9").Offset((9 * i) + 6, (3 * j)).Value

 Option Explicit Sub ChartSer_LineWidth() Dim i As Long, j As Long Dim MyCht As ChartObject ' set the chart object of Chart 6 to a variable Set MyCht = ActiveSheet.ChartObjects("Chart 6") For i = 0 To 1 For j = 0 To 11 With MyCht.Chart.SeriesCollection(12 * i + j + 1) .Format.Line.Visible = msoTrue .Format.Line.Weight = Sheet5.Range("Q9").Offset((9 * i), (3 * j)).Value End With Next j Next i End Sub 
 For Series = 1 To 24 'chart series, 144 combinations For i = 0 To 1 For j = 0 To 11 ActiveSheet.ChartObjects("Chart 4").Activate ActiveChart.FullSeriesCollection(Series).Select With Selection.Format.Line .Visible = msoTrue .Weight = Sheet5.Range("Q9").Offset((9 * i) + 6, (3 * j)).Value End With Next j Next i Next Series 

你必须用“EndWith”closures每个“With”。 这是在你的代码中省略。

用“Next”closures每个“if”和“EndIf”和“for”,以避免“编译错误: