Excel错误,停止运行宏

And*_*nez 5 excel vba excel-vba

我在Excel上遇到了一个奇怪的错误.当我按CTRL + m(宏快捷方式)时,我有一个显示非模态用户窗体的宏.每隔一段时间,它不是那么频繁(白天显示一次或两次,我每5分钟左右使用一次宏),Excel将不会运行宏,不会显示用户形式,只会发出蜂鸣声(如"错误,无法继续执行代码").

我进入宏窗口尝试按"运行"并手动执行,但所有按钮都被禁用,"创建"除外.如果单击它,则表示宏名称无效.正如您在下面的屏幕截图中看到的,宏的名称显示了代码所在的实例(工作簿的Sheet1).

有时它可以通过保存工作簿并重新尝试来修复,但有时它不会; 当它没有时,我运行一个不同的宏(通过双击特定列),显示模态用户窗体,并执行其代码.然后我的第一个宏恢复正常.

任何帮助将非常感谢.

宏运行窗口

编辑:根据评论中的请求添加代码

Sub ShowCommentWindow()
    Dim myCell As Range
    Dim companyColumn As Long
    Dim wbk as Workbook
    Dim company as String
    Dim phone as Long

    Set wbk = ActiveWorkbook

For Each myCell In wbk.Worksheets(1).Range("A1:Q1")

   If myCell.Text = "Company" Then
        companyColumn = myCell.Column
        company = ActiveCell.Text
        phone = ActiveCell.Offset(0, 4).Value
        Exit For
    End If
Next myCell

If ActiveCell.Column = companyColumn Then

    If EmailForm.Visible Then
        GoTo ExitProc
    Else
        If Not ActiveCell.Row < 4 Then
            ActiveWindow.ScrollRow = ActiveCell.Row - 3
        Else
            ActiveWindow.ScrollRow = ActiveCell.Row
        End If
        If CommentWindow.Visible Then
            CommentWindow.AddButton.SetFocus
            CommentWindow.CommentBox.SetFocus
            Exit Sub
        Else
            CommentWindow.Show
            ManageComments
            AddComment
        End If
    End If
End If

ExitProc:
End Sub
Run Code Online (Sandbox Code Playgroud)

Edit2:为QueryClose发布更多代码:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

Dim myCell As Range
Dim isCompany As String

    If Not CommentWindow.CommentBox.Text = CommentWindow.TextCopy.Text Then
        saveConf = MsgBox("Changes have not been saved yet. Do you want to save?", vbExclamation + vbYesNoCancel + vbDefaultButton2, "Save changes?")
        If saveConf = vbYes Then
            Call SaveComment
            GoTo ExitProc
        ElseIf saveConf = vbCancel Then
            changed = True
            Cancel = 1
            CommentWindow.AddButton.SetFocus
            CommentWindow.CommentBox.SetFocus
            'CommentWindow.CommentBox.Text = CommentWindow.TextCopy.Text
        Else
            CommentWindow.TextCopy.Text = CommentWindow.CommentBox.Text
            GoTo ExitProc
        End If
    Else
        If Not changed = True Then
            GoTo ExitProc
        End If
    End If

ExitProc:
End Sub
Run Code Online (Sandbox Code Playgroud)

Sgd*_*dva 2

Unload(UserForm)似乎问题不在于从 ( ) 卸载表单,
这会导致内存泄漏。
即使官方文档- 这是指 Access,但是对于 Excel 的行为应该相同(那里没有 Form 对象或用户窗体文档) - 声明生命周期是卸载 - >停用 - >关闭,这应该在关闭用户窗体时发生此外,日常使用表明,如果未声明,在关闭用户窗体时可能不会触发卸载。
有时生命周期并没有受到严格监控,但是,这可能会导致内存泄漏和奇怪的行为,在处理对象时,如果未指定,您不应该依赖垃圾收集器来清理它们。添加一些内容来确认终止是否被正确处理可能会有帮助。
编辑
如果您在记住卸载时遇到问题 - 或者仍然存在内存问题 - ,执行以下操作将是一个很好的做法:

Sub MyMainProcess()
Dim myform As UserForm1: Set myform = UserForm1 'this is your UserForm name
myform.Show
'my stuff needed...
Unload myform
Set myform = Nothing
End Sub
Run Code Online (Sandbox Code Playgroud)

通过编码尽可能卸载并清除任何东西