我在Excel上遇到了一个奇怪的错误。当我按CTRL+m (宏快捷方式)时,我有一个显示非模态用户表单的宏。每隔一段时间,它就不会那么频繁了(一天中会出现一两次,我每5分钟左右使用一次宏),Excel不会运行宏,不会显示userform,只会发出嘟嘟声(如“错误,不能继续执行代码”)。
我进入宏窗口尝试按"Run“并手动执行,但除"Create”外,所有按钮都被禁用。如果单击它,它会显示宏名无效。正如您在下面的屏幕截图中所看到的,宏的名称显示了代码所在的实例(工作簿的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
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
发布于 2016-06-27 17:29:49
我看到您正在调用一个“外部”宏(它不在活动工作簿中)--那么,那些每天大约有2次工作簿(数据库2 Lumber.xlsm)不工作的宏是否可能被其他人使用(运行该宏的是8个宏,还是另一个宏?)。
如果是这样的话,我以前所做的就是每次运行宏时都保存工作簿的本地副本。
https://stackoverflow.com/questions/35636661
复制相似问题