简体   繁体   English

下一步,没有for for in nested for循环,excel VBA,错误

[英]Next without for in nested for loop, excel VBA , error

I have 3 nested loops within each other. 我彼此之间有3个嵌套循环。 the excel sheet has 3 sheets of accident data. Excel工作表包含3个事故数据表。 For example the first sheet has 1 accident involving 2 cars which would be in the second sheet (second sheet has 2 vehicles for 1 accident). 例如,第一张纸有1次涉及2辆车的事故,而第二张纸将在第二张纸中(第二张有2辆车发生1次事故)。 The third sheet contains passenger details ( 2 passenger each therefore 4 rows for each accident). 第三页包含乘客详细信息(每位2位乘客,因此每次事故4行)。 I am getting a next without for in this case. 在这种情况下,我将获得下一个。 I am not sure where i went wrong. 我不确定我哪里出错了。 I would like assistance in this matter. 在这件事上,我需要协助。

Public Function table13()
    Dim acctyp, n, tv, toi, t1(8), t2(8), t3(8), t4(8), t5(8), t6(8), t7(8) As Integer

    n = ThisWorkbook.Worksheets(7).Cells(Rows.Count, "A").End(xlUp).Row

    For i = 0 To 7
        t1(i) = 0
        t2(i) = 0
        t3(i) = 0
        t4(i) = 0
        t5(i) = 0
        t6(i) = 0
        t7(i) = 0

    Next i

    For i = 2 To n
        acctyp = ThisWorkbook.Worksheets(7).Cells(i, "P").Value
        For veh = 2 To ThisWorkbook.Worksheets(10).Cells(Rows.Count, "A").End(xlUp).Row
            If ThisWorkbook.Worksheets(10).Cells(j, "A").Value = ThisWorkbook.Worksheets(7).Cells(i, "A").Value Then
                tv = ThisWorkbook.Worksheets(10).Cells(j, "I").Value

                If acctyp = 1 Then
                    If tv = 1 Then
                        t1(0) = t1(0) + 1
                    ElseIf tv = 2 Then
                        t2(0) = t2(0) + 1
                    ElseIf tv = 3 Then
                        t3(0) = t3(0) + 1
                    ElseIf tv = 4 Then
                        t4(0) = t4(0) + 1
                    ElseIf tv = 5 Then
                        t5(0) = t5(0) + 1
                    ElseIf tv = 6 Then
                        t6(0) = t6(0) + 1
                    ElseIf tv = 99 Then
                        t7(0) = t7(0) + 1
                    End If

                ElseIf acctyp = 2 Then
                    If tv = 1 Then
                        t1(1) = t1(1) + 1
                    ElseIf tv = 2 Then
                        t2(1) = t2(1) + 1
                    ElseIf tv = 3 Then
                        t3(1) = t3(1) + 1
                    ElseIf tv = 4 Then
                        t4(1) = t4(1) + 1
                    ElseIf tv = 5 Then
                        t5(1) = t5(1) + 1
                    ElseIf tv = 6 Then
                        t6(1) = t6(1) + 1
                    ElseIf tv = 99 Then
                        t7(1) = t7(1) + 1
                    End If

                ElseIf acctyp = 3 Then
                    If tv = 1 Then
                        t1(2) = t1(2) + 1
                    ElseIf tv = 2 Then
                        t2(2) = t2(2) + 1
                    ElseIf tv = 3 Then
                        t3(2) = t3(2) + 1
                    ElseIf tv = 4 Then
                        t4(2) = t4(2) + 1
                    ElseIf tv = 5 Then
                        t5(2) = t5(2) + 1
                    ElseIf tv = 6 Then
                        t6(2) = t6(2) + 1
                    ElseIf tv = 99 Then
                        t7(2) = t7(2) + 1
                    End If

                ElseIf acctyp = 4 Then
                    If tv = 1 Then
                        t1(3) = t1(3) + 1
                    ElseIf tv = 2 Then
                        t2(3) = t2(3) + 1
                    ElseIf tv = 3 Then
                        t3(3) = t3(3) + 1
                    ElseIf tv = 4 Then
                        t4(3) = t4(3) + 1
                    ElseIf tv = 5 Then
                        t5(3) = t5(3) + 1
                    ElseIf tv = 6 Then
                        t6(3) = t6(3) + 1
                    ElseIf tv = 99 Then
                        t7(3) = t7(3) + 1
                    End If

                End If

                toi = ThisWorkbook.Worksheets(10).Cells(j, "J").Value

                If toi = 1 Then
                    If tv = 1 Then
                        If tv = 1 Then
                            t1(5) = t1(5) + 1
                        ElseIf tv = 2 Then
                            t2(5) = t2(5) + 1
                        ElseIf tv = 3 Then
                            t3(5) = t3(5) + 1
                        ElseIf tv = 4 Then
                            t4(5) = t4(5) + 1
                        ElseIf tv = 5 Then
                            t5(5) = t5(5) + 1
                        ElseIf tv = 6 Then
                            t6(5) = t6(5) + 1
                        ElseIf tv = 99 Then
                            t7(5) = t7(5) + 1
                        End If

                    ElseIf toi = 2 Then
                        If tv = 1 Then
                            t1(6) = t1(6) + 1
                        ElseIf tv = 2 Then
                            t2(6) = t2(6) + 1
                        ElseIf tv = 3 Then
                            t3(6) = t3(6) + 1
                        ElseIf tv = 4 Then
                            t4(6) = t4(6) + 1
                        ElseIf tv = 5 Then
                            t5(6) = t5(6) + 1
                        ElseIf tv = 6 Then
                            t6(6) = t6(6) + 1
                        ElseIf tv = 99 Then
                            t7(6) = t7(6) + 1
                        End If

                    ElseIf toi = 3 Then
                        If tv = 1 Then
                            t1(7) = t1(7) + 1
                        ElseIf tv = 2 Then
                            t2(7) = t2(7) + 1
                        ElseIf tv = 3 Then
                            t3(7) = t3(7) + 1
                        ElseIf tv = 4 Then
                            t4(7) = t4(7) + 1
                        ElseIf tv = 5 Then
                            t5(7) = t5(7) + 1
                        ElseIf tv = 6 Then
                            t6(7) = t6(7) + 1
                        ElseIf tv = 99 Then
                            t7(7) = t7(7) + 1
                        End If
                    End If

                    For pass = 2 To ThisWorkbook.Worksheets(11).Cells(Rows.Count, "A").End(xlUp).Row
                        If ThisWorkbook.Worksheets(11).Cells(k, "A").Value = ThisWorkbook.Worksheets(9).Cells(j, "A").Value Then
                            If ThisWorkbook.Worksheets(11).Cells(k, "C").Value = ThisWorkbook.Worksheets(10).Cells(j, "C").Value Then
                                toi = ThisWorkbook.Worksheets(11).Cells(k, "I").Value
                                If toi = 1 Then
                                    If tv = 1 Then
                                        If tv = 1 Then
                                            t1(5) = t1(5) + 1
                                        ElseIf tv = 2 Then
                                            t2(5) = t2(5) + 1
                                        ElseIf tv = 3 Then
                                            t3(5) = t3(5) + 1
                                        ElseIf tv = 4 Then
                                            t4(5) = t4(5) + 1
                                        ElseIf tv = 5 Then
                                            t5(5) = t5(5) + 1
                                        ElseIf tv = 6 Then
                                            t6(5) = t6(5) + 1
                                        ElseIf tv = 99 Then
                                            t7(5) = t7(5) + 1
                                        End If

                                    ElseIf toi = 2 Then
                                        If tv = 1 Then
                                            t1(6) = t1(6) + 1
                                        ElseIf tv = 2 Then
                                            t2(6) = t2(6) + 1
                                        ElseIf tv = 3 Then
                                            t3(6) = t3(6) + 1
                                        ElseIf tv = 4 Then
                                            t4(6) = t4(6) + 1
                                        ElseIf tv = 5 Then
                                            t5(6) = t5(6) + 1
                                        ElseIf tv = 6 Then
                                            t6(6) = t6(6) + 1
                                        ElseIf tv = 99 Then
                                            t7(6) = t7(6) + 1
                                        End If

                                    ElseIf toi = 3 Then
                                        If tv = 1 Then
                                            t1(7) = t1(7) + 1
                                        ElseIf tv = 2 Then
                                            t2(7) = t2(7) + 1
                                        ElseIf tv = 3 Then
                                            t3(7) = t3(7) + 1
                                        ElseIf tv = 4 Then
                                            t4(7) = t4(7) + 1
                                        ElseIf tv = 5 Then
                                            t5(7) = t5(7) + 1
                                        ElseIf tv = 6 Then
                                            t6(7) = t6(7) + 1
                                        ElseIf tv = 99 Then
                                            t7(7) = t7(7) + 1
                                        End If
                                    End If
                                End If
                            End If
                        Next pass
                    End If
                Next veh
            Next i
        End Function

sticking to the very subject of the question you missed closing an "If-Then" block 坚持问题的主题,您错过了关闭“ If-Then”块

you have: 你有:

            ...
            toi = ThisWorkbook.Worksheets(10).Cells(j, "J").Value

            If toi = 1 Then
                If tv = 1 Then
                    If tv = 1 Then
                    t1(5) = t1(5) + 1  '<<=== missing End If
                ElseIf tv = 2 Then
            ...    

one possible fix is: 一种可能的解决方法是:

            ...
            toi = ThisWorkbook.Worksheets(10).Cells(j, "J").Value

            If toi = 1 Then
                If tv = 1 Then
                    If tv = 1 Then t1(5) = t1(5) + 1  
                ElseIf tv = 2 Then
            ... 

another possible fix is: 另一个可能的解决方法是:

            ...
            If toi = 1 Then
                If tv = 1 Then
                    If tv = 1 Then
                        t1(5) = t1(5) + 1 
                    End If
                ElseIf tv = 2 Then
            ...    

While trying you may consider adopting a 2D array and Select Case syntax to simplify your code 尝试时,您可以考虑采用2D数组和Select Case语法来简化代码

for example: 例如:

Dim acctyp As Long, n As Long, i As Long, j As Long, tv As Long, toi As Long, t(1 To 7, 0 To 7) As Long '-<<== use a 2D t(,) array of `Long` s

n = ThisWorkbook.Worksheets(7).Cells(Rows.Count, "A").End(xlUp).Row

' just for demonstrative purposes, while you may skip this double loop since an array of Long type gets initialized with zeros by default
For i = 1 To 7
    For j = 0 To 7
        t(i, j) = 0
    Next
Next

For i = 2 To n
    acctyp = ThisWorkbook.Worksheets(7).Cells(i, "P").Value
    For veh = 2 To ThisWorkbook.Worksheets(10).Cells(Rows.Count, "A").End(xlUp).Row
        If ThisWorkbook.Worksheets(10).Cells(j, "A").Value = ThisWorkbook.Worksheets(7).Cells(i, "A").Value Then
            tv = ThisWorkbook.Worksheets(10).Cells(j, "I").Value

            Select Case acctyp
                Case 1 To 4
                Select Case tv
                    Case 1 To 6
                        t(tv, acctyp - 1) = t(tv, acctyp - 1) + 1
                    Case 99
                        t(7, acctyp - 1) = t(7, acctyp - 1) + 1
                End Select
            End Select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM