0

So I am very new to coding to VBA or to programming in general. I am receiving a "Compile error: Next without for". I believe I am giving 3 nexts for 3 fors but still I have no clue. Below is the code I am working on....

Sub width2() 'to assign series width automatically

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

Not sure where the mistake is. Any help much appreciated Thank you.

10
  • 3
    you are missing End With to your With Selection.Format.Line Commented Jan 2, 2017 at 10:02
  • May be problem in With method. You do not close With. You have to use End With before Next j. Commented Jan 2, 2017 at 10:02
  • After adding end with before next j , I am now getting : "The item with the specified name was not found" for line: ActiveSheet.ChartObjects("Chart 4").Activate Commented Jan 2, 2017 at 10:27
  • 1
    Make shure there is a chart named Chart 4 in your active sheet... Commented Jan 2, 2017 at 10:44
  • @PiyushVerma please update your code with the End With , so we will remove that error from you post. Secod, you need to take your ActiveSheet.ChartObjects("Chart 4").Activate and the following line from inside the nested For loops to be the first line in your Sub. Just keep the .Weight = Sheet5.Range("Q9").Offset((9 * i) + 6, (3 * j)).Value inside Commented Jan 2, 2017 at 12:14

3 Answers 3

1

Since you didn't share your Worksheet Data structure, this is not the full answer.

The code runs without the need to use Activate, ActiveChart and Select. Instead it references the "Chart 6" ChartObject and then modifies the Line.Visible and Line.Weight properites of the SeriesCollection using the following With statement: With MyCht.Chart.SeriesCollection(12 * i + j + 1).

Note: if @Piyush Verma decides to share his data structue, also the math part of Sheet5.Range("Q9").Offset((9 * i) + 6, (3 * j)).Value can be handled.

Code

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
Sign up to request clarification or add additional context in comments.

1 Comment

you're welcome, please mark as answer (click the little V next to my answer)
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

Comments

0

You have to close every "With" with "EndWith". That is ommitted in your code.

Also close every "if" with "EndIf" and "for" with "Next", to avoid "Compile error: Next without for"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.