VBA - 获取子或函数未定义错误,错误处理处于活动状态

时间:2017-08-31 05:54:39

标签: vba error-handling

我有一个谜。在我做了一段时间的项目中,当你激活另一张表时会有一个检查(“Worksheet_Activate”事件)。检查是查看“UnsaVedList()”字符串数组中是否有任何内容,这意味着存在未保存的更改。这个变量在早期就被重新修改了,但我想它可能会以某种方式消失而不再被确定 - 我没有得到那个部分,但这在这里甚至不是问题。

问题在于,当至少一个用户的计算机点击该行代码时,它会检查该变量的第一个库,用户因为没有定义变量而导致错误。在“On Error GoTo label”行之后只有2行,该行在错误弹出时仍然有效。

知道如何发生这种情况或如何解决这个问题?我将发布下面的整个子

Error image

Private Sub Worksheet_Activate()
Dim dontSave As Integer, unsavedDay As Date, backSheet As Worksheet
Set backSheet = ActiveSheet
On Error GoTo noUnsaved
Application.EnableEvents = False
    If Not UnsaVedList(0) = Empty Then
        unsavedDay = DateSerial(Int(Left(UnsaVedList(0), 4)),Int(Mid(UnsaVedList(0), 5, 2)), Int(Mid(UnsaVedList(0), 7, 2)))
        Sheets(18 - DateDiff("d", unsavedDay, Sheets(5).Cells(2, 1).Value)).Activate
        dontSave = MsgBox("You have unsaved changes on " & Format(unsavedDay, "ddd d mmm") & ". Click Yes to review the changes or No to abandon the changes on this day", vbYesNo, "Review changes?")
        Select Case dontSave
            Case 6 'yes
                dontSave = ActiveSheet.Columns("A:A").Find(What:=Int(Right(UnsaVedList(0), 2)), After:=Cells(1, 1), LookIn:=xlFormulas, LookAt _
                :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                False, SearchFormat:=False).Row
                ActiveSheet.Range(ActiveSheet.Cells(dontSave, 3), ActiveSheet.Cells(ActiveSheet.Cells(dontSave, 1).End(xlDown).Row, 14)).Select
            Case 7 'no
                ReDim UnsaVedList(0)
                backSheet.Activate
        End Select
    End If
noUnsaved: '''<< this is where it's supposed to jump to!
On Error GoTo 0
Application.EnableEvents = True
End Sub

0 个答案:

没有答案