无法解决运行-time error 92: For loop not initialized。任何的想法?

Can't solve Run-time error 92: For loop not initialized. Any idea?


我有一个小循环来检查所有工作表和过去,如果满足某些条件,则复制一列。但是,在完成所有工作表后,我 运行 变成了 "Run-time error 92: For loop not initialized"。我知道,只要循环是 运行(直到最后一个工作表)或发生错误,就会解决 Next ws。我不明白为什么在循环完成后使用 Next ws
你知道我做错了什么吗?
非常感谢!

Dim MonthBC As String
Dim YearBC As String
Dim Phase As String
Dim colBC As Long
Dim colNo As Long
Dim vCol As Variant
Dim coli As Long

MonthBC = Form_Start_Update.ComboBox_Month.Value
YearBC = Form_Start_Update.ComboBox_Year.Value
Phase = "Plan"

For Each ws In ThisWorkbook.Worksheets
    With ws
        Debug.Print ws.Range("A1").Parent.Name
        colNo = ws.Cells(8, Columns.Count).End(xlToLeft).Column
        vCol = Application.WorksheetFunction.Transpose(ws.Range(Cells(8, 1).Address, Cells(10, colNo).Address).Value2)
        If colNo = 1 Then
        GoTo Continue_Next
        Else
            For coli = LBound(vCol, 1) To UBound(vCol, 1)
                On Error GoTo Continue_Next
                If IsDate(vCol(coli, 1)) = True Then
                    vCol(coli, 1) = Year(vCol(coli, 1))
                End If
                If vCol(coli, 1) = YearBC Then
                    If vCol(coli, 2) = MonthBC Then
                        If vCol(coli, 3) = Phase Then
                        colBC = coli
                        ws.Range(Cells(1, colBC + 1).Address).EntireColumn.Insert
                        ws.Range(Cells(1, colBC).Address, Cells(Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row, colBC).Address).Copy
                        ws.Range(Cells(1, colBC + 1).Address).PasteSpecial Paste:=xlPasteValues
                        ws.Range(Cells(11, 10).Address, Cells(Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row, 10).Address).Copy
                        ws.Range(Cells(11, colBC).Address).PasteSpecial Paste:=xlPasteFormulas
                        End If
                    End If
                End If
            Next coli
        End If
    End With

Continue_Next:
    Next ws

问题在于在循环内使用标签。更改代码的逻辑并摆脱它们。仅供参考,您需要在错误时转到标签时重置错误处理程序,但同样,无论如何您都想摆脱标签。

/e:更准确地说,on error goto Continue_Next 转到循环外的标签,我认为这是导致错误的原因。如果您预计此处会出现错误,请将 On Error Resume Next 放在顶部,然后测试您的错误 If Err.Number > 0 then Exit For - 不需要标签。记得清除错误和错误处理程序。尝试这样的事情:

For Each ws In ThisWorkbook.Worksheets

    Debug.Print ws.Range("A1").Parent.Name
    colNo = ws.Cells(8, Columns.count).End(xlToLeft).Column
    vCol = Application.WorksheetFunction.Transpose(ws.Range(Cells(8, 1).Address, Cells(10, colNo).Address).Value2)

    If colNo > 1 Then
        For coli = LBound(vCol, 1) To UBound(vCol, 1)
            On Error Resume Next
            'test your error
            If Err.number > 0 Then Exit For
            On Error GoTo 0
            If IsDate(vCol(coli, 1)) = True Then vCol(coli, 1) = Year(vCol(coli, 1))
            If vCol(coli, 1) = YearBC Then
                If vCol(coli, 2) = MonthBC Then
                    If vCol(coli, 3) = Phase Then
                        colBC = coli
                        ws.Range(Cells(1, colBC + 1).Address).EntireColumn.Insert
                        ws.Range(Cells(1, colBC).Address, Cells(Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).row, colBC).Address).Copy
                        ws.Range(Cells(1, colBC + 1).Address).PasteSpecial Paste:=xlPasteValues
                        ws.Range(Cells(11, 10).Address, Cells(Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).row, 10).Address).Copy
                        ws.Range(Cells(11, colBC).Address).PasteSpecial Paste:=xlPasteFormulas
                    End If
                End If
            End If
        Next coli
        On Error GoTo 0
    End If
Next ws