Excel错误,停止运行macros

我在Excel上遇到一个奇怪的错误。 我有一个macros,显示一个非模态的用户表单,当我按CTRL + m(macros快捷键)。 每隔一段时间,并不是那么频繁(白天出现一次或两次,每5分钟左右使用一次macros),Excel将不会运行macros,不会显示用户窗体,只会发出蜂鸣声(如“错误,不能执行代码”)。

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

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

任何帮助将非常感激。

宏运行窗口

编辑:添加注释中所要求的代码

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 

编辑2:发布更多的代码,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 

似乎问题不是从( Unload(UserForm)Unload(UserForm)表单
这导致内存泄漏。
即使是官方文档 – 这指的是访问,但是,应该performance为相同的Excel(没有表单对象或用户窗体文档那里) – 声明生命周期是卸载 – >停用 – >closures,这应该发生在closures用户窗体同时,日常使用情况表明,如果没有说明,则在closures用户表单时可能不会触发卸载。
生命周期有时并不严格监控,但是,这可能会导致内存泄漏和奇怪的行为,总是在处理对象时,不应该依赖垃圾收集器,如果没有指定,它们将清理它们。 大概添加一些东西,以确认终止正确处理将是有益的。
编辑
如果您在记忆卸载时遇到问题 – 或者仍然存在内存问题,那么执行以下操作将是一个很好的做法:

 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 

尽可能地用编码来卸载和清除任何东西

我发现你正在调用一个“外部”macros(它不在活动工作簿内) – 那么这个工作簿(Database 2 Lumber.xlsm)是不是可以使用那些大约每天2次的工作簿由当时的其他人(八个运行那个,还是另一个macros?)。

如果是这样的话,我以前所做的是每次运行macros时保存工作簿的本地副本