2017-01-02 115 views
0

所以我對編碼到VBA或一般編程很新。我正在收到「編譯錯誤:未找到」。我相信我會爲3場比賽奉獻3場比賽,但我仍然沒有任何線索。下面是我的....如何解決VBA中的「編譯錯誤:next for for」錯誤?

子寬2使用的代碼()來分配一系列寬度自動

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 

不知道其中的錯誤是。 非常感謝任何幫助 謝謝。

+3

你缺少'結束With'你'隨着Selection.Format .Line' –

+0

可能會出現問題'With'方法。你不用'關閉'。你必須在下一個j之前使用'End With'。 – harun24hr

+0

在下一個j之前添加結束後,我現在得到:「沒有找到具有指定名稱的項目」對於行:ActiveSheet.ChartObjects(「Chart 4」)。激活 –

回答

1

既然你沒有分享你的工作表數據結構,這不是完整的答案。

該代碼運行時無需使用Activate,ActiveChartSelect。相反,它引用「圖表6」ChartObject,然後使用以下With語句修改Line.VisibleLine.Weight合適的SeriesCollectionWith MyCht.Chart.SeriesCollection(12 * i + j + 1)

注意:如果@Piyush Verma決定分享他的數據結構,也可以處理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 
+0

謝謝謝:)它的工作 –

+0

歡迎您,請標記爲答案(點擊我的答案旁邊的小** V **) –

1
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 
0

您必須關閉所有的 「與」 用 「ENDWITH」。這是在你的代碼中省略的。

附近還有每一個「如果」和「ENDIF」和「下一步」,「對」,避免「編譯錯誤:沒有爲下一步」