循环语法问题

时间:2016-06-24 13:15:05

标签: excel vba excel-vba loops syntax

我正在编写一系列循环来逐行搜索一个表中的数据,如果它符合该行中的正确规范,它将从所述行中提取一个值并将其放在我想要的单元格中另一张图表上的表格。我收到的错误是说有一个"下一个"没有" For",但有(除非我完全遗漏了一些东西)。它还突出显示顶部的Sub行(我试图将其作为宏运行)。让我知道我在哪里出错了,谢谢!

Option Explicit

Sub GraphLoop()

Dim i, g, row, color, roundcount, round, materialcount, material As Long
Dim chartdatacol, chartdatarow As Long




Worksheets("Chart Data").Range("C6:DR10000").Value = ""

        roundcount = 1
        materialcount = 1
        color = 1
        round = 1
        material = 1
        chartdatacol = 3
        chartdatarow = 6
        i = 4

For chartdatacol = 3 To Worksheets("Running Avg Log").Cells(4, cols.Count).End(xlUp).row

Do Until i = Worksheets("Running Avg Log").Cells(Rows.Count, "A").End(xlUp).row


        g = 1



        If Worksheets("Running Avg Log").Cells(i, 1).Value = Worksheets("Chart Data").Cells(chartdatarow, 2).Value _
            And Worksheets("Running Avg Log").Cells(i, 2).Value = round _
            And Worksheets("Running Avg Log").Cells(i, 3).Value = color _
            And Worksheets("Running Avg Log").Cells(i, 4).Value = material Then

            row = chartdatarow

            Worksheets("Chart Data").Cells(row, chartdatacol).Value = _
            Worksheets("Running Avg Log").Cells(i, 6 + Worksheets("Analysis").Range("C5").Value).Value


            chartdatarow = chartdatarow + 1
            i = 4


        Else

            i = i + 1


        End If



Loop


    color = color + 1

    ' loops through ten colors
        If color > 10 Then

            color = 1

        End If

    roundcount = roundcount + 1
    materialcount = materialcount + 1

    ' every ten columns, material changes, every 30 columns, it repeats
        If materialcount = 11 Or 21 Then

            material = material + 1

        End If

        If materialcount > 30 Then

            materialcount = 1
            material = 1

    ' each round's 30 combos is 30 columns
        If roundcount = 31 Then

            round = round + 1

        End If


Next chartdatacol




End Sub

2 个答案:

答案 0 :(得分:0)

你遗漏了一个结束如果在这里:

If materialcount > 30 Then

        materialcount = 1
        material = 1

' each round's 30 combos is 30 columns
BTW,另一项更正:

    If materialcount = 11 Or 21 Then

应该是:

    If materialcount = 11 Or materialcount = 21 Then

答案 1 :(得分:0)

您的If没有End If

请参阅下面的修改后的代码,我在*********中标记了您错过了End If:

的地方
Option Explicit
Dim i, g, row, color, roundcount, round, materialcount, material    As Long
Dim chartdatacol, chartdatarow                                      As Long


Sub GraphLoop()


Worksheets("Chart Data").Range("C6:DR10000").Value = ""

    roundcount = 1
    materialcount = 1
    color = 1
    round = 1
    material = 1
    chartdatacol = 3
    chartdatarow = 6
    i = 4

    For chartdatacol = 3 To Worksheets("Running Avg Log").Cells(4, Columns.Count).End(xlUp).row

        Do Until i = Worksheets("Running Avg Log").Cells(Rows.Count, "A").End(xlUp).row
            g = 1
            If Worksheets("Running Avg Log").Cells(i, 1).Value = Worksheets("Chart Data").Cells(chartdatarow, 2).Value _
                And Worksheets("Running Avg Log").Cells(i, 2).Value = round _
                And Worksheets("Running Avg Log").Cells(i, 3).Value = color _
                And Worksheets("Running Avg Log").Cells(i, 4).Value = material Then

                row = chartdatarow

                Worksheets("Chart Data").Cells(row, chartdatacol).Value = _
                Worksheets("Running Avg Log").Cells(i, 6 + Worksheets("Analysis").Range("C5").Value).Value
                chartdatarow = chartdatarow + 1
                i = 4
            Else
                i = i + 1
            End If
        Loop

        color = color + 1

        ' loops through ten colors
        If color > 10 Then
            color = 1
        End If

        roundcount = roundcount + 1
        materialcount = materialcount + 1

        ' every ten columns, material changes, every 30 columns, it repeats
        If materialcount = 11 Or 21 Then
            material = material + 1
        End If

        If materialcount > 30 Then
            materialcount = 1
            material = 1
            ' each round's 30 combos is 30 columns
            If roundcount = 31 Then
                round = round + 1
            ' **** YOU WERE MISSING the End If here ***
            End If
        End If
    Next chartdatacol

End Sub