语法/循环错误

时间:2016-06-22 12:19:47

标签: excel vba excel-vba syntax

我正在编写一个嵌套循环来遍历我所拥有的表的每个单元格(行和列)。每个细胞都有一个独特的组合"它必须等于,如果它匹配我在另一个工作表上引用的值,它将从该同一行获取一个值(在Analysis工作表中指定)并在此表中返回它。这是我的代码。我在较大的If语句中收到错误。任何帮助表示赞赏。谢谢!

更新了代码,所有作品除了最后一个"并且......"在if语句中。没有它,代码运行,但不正确(出于显而易见的原因)。当我把它包括进去时,excel冻结并且它永远不会完成运行。请帮忙。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim i,j,k,m,n As Long

工作表("图表数据")。范​​围(" C6:DR10000")。值=""

j = 3

Do Until Worksheets("Chart Data").Cells(4, j).Value = ""

    For i = 4 To Worksheets("Chart Data").Cells(Rows.Count, "A").End(xlUp).Row

        k = i + 3
        m = i + 2
        n = 1

        ThisWorkbook.Sheets("Running Avg Log").Activate
        ' If the current row in running avg log doesnt meet this if statement criteria, it skips & increments i w/o doing anything
        If Worksheets("Running Avg Log").Cells(i, 2).Value = 1 _
            And Worksheets("Running Avg Log").Cells(i, 3).Value = n _
            And Worksheets("Running Avg Log").Cells(i, 4).Value = 1 _
            And Worksheets("Running Avg Log").Cells(i, 1).Value = Worksheets("Chart Data").Cells(k, 2).Value Then

            ' When if statement is entered, this sets the selected dimension # in this accepted row and puts it into corresponding spot in chart data
            Worksheets("Chart Data").Cells(m, j).Value = Worksheets("Running Avg Log").Cells(i, 6 + Worksheets("Analysis").Range("C5").Value).Value

            n = n + 1

        End If
    Next i

    ' j (column number) will increment across after each row in one column is done, testing the entire table
    j = j + 1
Loop

End Sub

sheet where code will input the values

sheet where the values are found. If statement looks for date, and the three values following the date. If they match what i want, it returns one of the dimension numbers in that same row. The dimension number is a drop down menu cell selected in the Analysis worksheet, and can be referenced with the cell I have shown in the line within the If statement.

1 个答案:

答案 0 :(得分:0)

以下更新的代码段无需抛出错误即可运行。 但是,不知道你想要代码做什么? 你桌子的价值究竟在哪里?你想把它们放在哪里?

尝试附上您尝试获取的所需工作表结果的屏幕截图。

Sub GraphLoop()

Dim i, j, k, m                          As Long

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

j = 3
    Do Until Worksheets("Chart Data").Cells(4, j).Value = ""
        For i = 4 To Worksheets("Chart Data").Cells(Rows.count, "B").End(xlUp).row  ' modify according to your Column                k = i + 3
            m = i + 2

            ThisWorkbook.Sheets("Running Avg Log").Activate
            ' If the current row in running avg log doesnt meet this if statement criteria, it skips & increments i w/o doing anything
            If Worksheets("Running Avg Log").Cells(i, 2).Value = 1 _
                And Worksheets("Running Avg Log").Cells(i, 3).Value = 1 _
                And Worksheets("Running Avg Log").Cells(i, 4).Value = 1 _
                And Worksheets("Running Avg Log").Cells(i, 1).Value = Worksheets("Chart Data").Cells(k, 1).Value Then

                ThisWorkbook.Worksheets("Chart Data").Activate
                ' When if statement is entered, this sets the selected dimension # in this accepted row and puts it into corresponding spot in chart data
                Worksheets("Chart Data").Cells(m, j).Value = Worksheets("Running Avg Log").Cells(i, 6 + Worksheets("Analysis").Range("C5").Value).Value
            End If
        Next i

        ' j (column number) will increment across after each row in one column is done, testing the entire table
        j = j + 1
    Loop

End Sub