我在Excel上遇到了一个奇怪的错误。我有一个宏,当我按下CTRL+m(宏快捷键)时,会显示一个非模态用户窗体。偶尔会出现一次或两次(一天中出现一两次,我每5分钟左右使用宏),Excel不会运行宏,不会显示用户窗体,只会发出嘟声(表示“错误,无法继续执行代码”)。
我进入宏窗口尝试按“运行”手动执行,但所有按钮都被禁用,除了“创建”按钮。如果您单击它,它会说宏名称无效。如下面的屏幕截图所示,宏的名称显示代码实例(工作簿的Sheet1)。
有时可以通过保存工作簿并重新尝试来解决,但有时候不行。在这种情况下,我运行另一个宏(通过双击特定列),显示一个模态用户窗体,并执行其代码。然后我的第一个宏就恢复正常了。
非常感谢任何帮助。
我进入宏窗口尝试按“运行”手动执行,但所有按钮都被禁用,除了“创建”按钮。如果您单击它,它会说宏名称无效。如下面的屏幕截图所示,宏的名称显示代码实例(工作簿的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
编辑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