Excel 错误奇怪行为的VBA分析

Excel 错误奇怪行为的VBA分析,excel,vba,onerror,Excel,Vba,Onerror,我在VBA编程方面很有经验,但这个问题偶尔会出现,即使在研究之后,我也从未找到这个问题的答案 这是我的代码片段 Dim sht As Worksheet For Each sht In Worksheets On Error GoTo Nextsheet col.Add sht.name, sht.name '<-- will be using it as a main Line in my post. shtList.A

我在VBA编程方面很有经验,但这个问题偶尔会出现,即使在研究之后,我也从未找到这个问题的答案

这是我的代码片段

    Dim sht As Worksheet

    For Each sht In Worksheets
        On Error GoTo Nextsheet

        col.Add sht.name, sht.name '<-- will be using it as a main Line in my post.
        shtList.AddItem sht.name
        shtList.List(shtList.ListCount - 1, 1) = sht.name
Nextsheet:
        Err.Clear
    Next sht
Dim sht作为工作表
对于工作表中的每个sht
转到下一页时出错

col.Add sht.name,sht.name'您需要像这样重写例程:

    Dim sht As Worksheet

    For Each sht In Worksheets
        On Error GoTo ErrHandler

        col.Add sht.name, sht.name '<-- will be using it as a main Line in my post.
        shtList.AddItem sht.name
        shtList.List(shtList.ListCount - 1, 1) = sht.name
Nextsheet:
        Err.Clear
    Next sht

    ' other code here

EndSub:
    exit sub

ErrHandler:
    Resume NextSht
End Sub
Dim sht作为工作表
对于工作表中的每个sht
关于错误转到错误处理程序

col.Add sht.name,sht.name'您需要像这样重写例程:

    Dim sht As Worksheet

    For Each sht In Worksheets
        On Error GoTo ErrHandler

        col.Add sht.name, sht.name '<-- will be using it as a main Line in my post.
        shtList.AddItem sht.name
        shtList.List(shtList.ListCount - 1, 1) = sht.name
Nextsheet:
        Err.Clear
    Next sht

    ' other code here

EndSub:
    exit sub

ErrHandler:
    Resume NextSht
End Sub
Dim sht作为工作表
对于工作表中的每个sht
关于错误转到错误处理程序

col.Add sht.name,sht.name'您需要像这样重写例程:

    Dim sht As Worksheet

    For Each sht In Worksheets
        On Error GoTo ErrHandler

        col.Add sht.name, sht.name '<-- will be using it as a main Line in my post.
        shtList.AddItem sht.name
        shtList.List(shtList.ListCount - 1, 1) = sht.name
Nextsheet:
        Err.Clear
    Next sht

    ' other code here

EndSub:
    exit sub

ErrHandler:
    Resume NextSht
End Sub
Dim sht作为工作表
对于工作表中的每个sht
关于错误转到错误处理程序

col.Add sht.name,sht.name'您需要像这样重写例程:

    Dim sht As Worksheet

    For Each sht In Worksheets
        On Error GoTo ErrHandler

        col.Add sht.name, sht.name '<-- will be using it as a main Line in my post.
        shtList.AddItem sht.name
        shtList.List(shtList.ListCount - 1, 1) = sht.name
Nextsheet:
        Err.Clear
    Next sht

    ' other code here

EndSub:
    exit sub

ErrHandler:
    Resume NextSht
End Sub
Dim sht作为工作表
对于工作表中的每个sht
关于错误转到错误处理程序

col.Add sht.name,sht.name'我认为问题在于,当错误处理程序仍然处于活动状态时,您收到了一个错误;由于没有已启用但处于非活动状态的错误处理程序,因此在发生错误时,该错误是致命的(有关错误声明,请参阅帮助)

我想尝试一下:

On Error Resume Next
    For Each sht In Worksheets
        col.Add sht.Name, sht.Name '<-- will be using it as a main Line in my post.
        If Err.Number = 0 Then
            shtList.AddItem sht.Name
            shtList.List(shtList.ListCount - 1, 1) = sht.Name
        Else
            Err.Clear
        End If
    Next sht
On Error Goto 0
出错时继续下一步
对于工作表中的每个sht

col.Add sht.Name,sht.Name'我认为问题在于,当错误处理程序仍然处于活动状态时,您收到了一个错误;由于没有已启用但处于非活动状态的错误处理程序,因此在发生错误时,该错误是致命的(有关错误声明,请参阅帮助)

我想尝试一下:

On Error Resume Next
    For Each sht In Worksheets
        col.Add sht.Name, sht.Name '<-- will be using it as a main Line in my post.
        If Err.Number = 0 Then
            shtList.AddItem sht.Name
            shtList.List(shtList.ListCount - 1, 1) = sht.Name
        Else
            Err.Clear
        End If
    Next sht
On Error Goto 0
出错时继续下一步
对于工作表中的每个sht

col.Add sht.Name,sht.Name'我认为问题在于,当错误处理程序仍然处于活动状态时,您收到了一个错误;由于没有已启用但处于非活动状态的错误处理程序,因此在发生错误时,该错误是致命的(有关错误声明,请参阅帮助)

我想尝试一下:

On Error Resume Next
    For Each sht In Worksheets
        col.Add sht.Name, sht.Name '<-- will be using it as a main Line in my post.
        If Err.Number = 0 Then
            shtList.AddItem sht.Name
            shtList.List(shtList.ListCount - 1, 1) = sht.Name
        Else
            Err.Clear
        End If
    Next sht
On Error Goto 0
出错时继续下一步
对于工作表中的每个sht

col.Add sht.Name,sht.Name'我认为问题在于,当错误处理程序仍然处于活动状态时,您收到了一个错误;由于没有已启用但处于非活动状态的错误处理程序,因此在发生错误时,该错误是致命的(有关错误声明,请参阅帮助)

我想尝试一下:

On Error Resume Next
    For Each sht In Worksheets
        col.Add sht.Name, sht.Name '<-- will be using it as a main Line in my post.
        If Err.Number = 0 Then
            shtList.AddItem sht.Name
            shtList.List(shtList.ListCount - 1, 1) = sht.Name
        Else
            Err.Clear
        End If
    Next sht
On Error Goto 0
出错时继续下一步
对于工作表中的每个sht


上校添加sht.Name,sht.Name‘宾果,我想你是对的,错误处理程序仍然处于活动状态。我之所以挠头,是因为一个原因,而不是因为担心找到解决办法。谢谢你的指点。我可以找到更多关于这个的信息。罗恩和皮特,还有一个问题。我今天发现了这种非常奇怪的行为。我可以在字典里加上两个相同的键…哇,我这辈子都没见过。可能是office 2013的一些bug?有什么想法吗?我使用On Error Resume Next来处理重复集合对象的“已存在”错误。我可以让代码“内联”而不是跳转到过程中的另一个位置。更容易理解(无论如何,对我来说)@Vikas我很少使用字典,但至少在2007年,字典中的重复键会导致运行时错误。宾果,我认为错误处理程序仍然处于活动状态是正确的。我之所以挠头,是因为一个原因,而不是因为担心找到解决办法。谢谢你的指点。我可以找到更多关于这个的信息。罗恩和皮特,还有一个问题。我今天发现了这种非常奇怪的行为。我可以在字典里加上两个相同的键…哇,我这辈子都没见过。可能是office 2013的一些bug?有什么想法吗?我使用On Error Resume Next来处理重复集合对象的“已存在”错误。我可以让代码“内联”而不是跳转到过程中的另一个位置。更容易理解(无论如何,对我来说)@Vikas我很少使用字典,但至少在2007年,字典中的重复键会导致运行时错误。宾果,我认为错误处理程序仍然处于活动状态是正确的。我之所以挠头,是因为一个原因,而不是因为担心找到解决办法。谢谢你的指点。我可以找到更多关于这个的信息。罗恩和皮特,还有一个问题。我今天发现了这种非常奇怪的行为。我可以在字典里加上两个相同的键…哇,我这辈子都没见过。可能是office 2013的一些bug?有什么想法吗?我使用On Error Resume Next来处理重复集合对象的“已存在”错误。我可以让代码“内联”而不是跳转到过程中的另一个位置。更容易理解(无论如何,对我来说)@Vikas我很少使用字典,但至少在2007年,字典中的重复键会导致运行时错误。宾果,我认为错误处理程序仍然处于活动状态是正确的。我之所以挠头,是因为一个原因,而不是因为担心找到解决办法。谢谢你的指点。我可以找到更多关于这个的信息。罗恩和皮特,还有一个问题。我今天发现了这种非常奇怪的行为。我可以在字典里加上两个相同的键…哇,我这辈子都没见过。可能是office 2013的一些bug?有什么想法吗?我使用On Error Resume Next来处理重复集合对象的“已存在”错误。我可以让代码“内联”而不是跳转到过程中的另一个位置。让我更容易理解(无论如何对我来说)@Vikas我很少使用字典,但至少在2007年,字典中的重复键会导致运行时错误。谢谢Peiter,我也会尝试。最后我也会给你+1的解释。谢谢谢谢佩特,我也会试试的。最后我也会给你+1的解释。谢谢谢谢佩特,我也会试试的。最后我也会给你+1的解释。谢谢谢谢佩特,我也会试试的。最后我也会给你+1的解释。谢谢