[英]Code execution has been interrupted
In Excel VBA, I am running into an "error" that halts the macro and a message displays "Code execution has been interrupted." 在Excel VBA中,我遇到了“错误”,该错误使宏停止,并且显示一条消息“代码执行已被中断”。 I wrote error in quotations because when I selected debug and examined the line of code that prompted the error, I saw that it was logically sound.
我用引号引起了错误,因为当我选择调试并检查了提示错误的代码行时,我发现它在逻辑上是合理的。
I originally ran into the error at On Error GoTo 0
. 我最初在
On Error GoTo 0
遇到On Error GoTo 0
。 When I comment out a block around the error, then I get a new line that produces the same error. 当我注释掉有关该错误的代码块时,我得到了一个产生相同错误的新行。 And, again, when I examine it in debug mode the new "error" is logically sound.
而且,再次,当我在调试模式下检查它时,新的“错误”在逻辑上是合理的。 Here is the exact line:
这是确切的行:
If rRange.Row <> 3 And rRange.Row <> 17 Then
FYI, rRange.Row = 3 in this case, so it shouldn't produce an error. 仅供参考,在这种情况下rRange.Row = 3,因此它不会产生错误。
Why is this happening and how can I fix it? 为什么会发生这种情况,我该如何解决?
UPDATE Code now produces the error on the End Sub
line. UPDATE代码现在在
End Sub
行上产生错误。
Here is the section that fails: 这是失败的部分:
Sub Review()
Dim WorkRange As Range
Dim FoundCells As Range
Dim Cell As Range
Dim a As String
Dim policy As String
Dim rRange As Range
Set RR = Sheets("Ready for Review")
Set OG = ActiveSheet
OG.Unprotect ("Password")
RR.Activate
On Error Resume Next
Application.DisplayAlerts = False
Set rRange = Application.InputBox(Prompt:= _
"Please select POLICY to review.", _
Title:="SPECIFY POLICY", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If rRange.Row <> 3 And rRange.Row <> 17 Then
MsgBox "Value other than a POLICY was selected. Select the cell that contains the correct policy number."
Exit Sub
Else
policy = rRange.Value
End If
Application.ScreenUpdating = False
OG.Cells(12, 2).Locked = False
Set WorkRange = OG.UsedRange
For Each Cell In WorkRange
If Cell.Locked = False Then
col1 = Cell.Column
Row = Cell.Row
a = OG.Cells(Row, 1)
If Not a = "" Then
row2 = Application.WorksheetFunction.Match(a, RR.Range("A:A"), 0)
Cell.Value = RR.Cells(row2, rRange.Column + col1 - 2)
End If
End If
Next Cell
OG.Unprotect ("Password")
OG.Cells(33, 3).Locked = False
If (Right(OG.Cells(5, 2), 2) = "UL" Or Right(OG.Cells(5, 2), 2) = "IL" Or Right(OG.Cells(5, 2), 2) = "PL") Then
With OG.Cells(33, 3)
.Value = "=IF(INDEX(B:B,MATCH(""Total*"",A:A,0))="""",0,INDEX(B:B,MATCH(""Total*"",A:A,0)))-SUM(C34:C37)"
.Locked = True
End With
ElseIf Right(OG.Cells(5, 2), 2) = "WL" Then
With OG.Cells(33, 3)
.Value = "=IF(INDEX(B:B,MATCH(""Total*"",A:A,0))="""",0,INDEX(B:B,MATCH(""*"",A:A,0))) - IFERROR(INDEX(C34:C37,MATCH(""Additional"",B34:B37, 0)),0) - IFERROR(INDEX(C34:C37,MATCH(""Paid"",B34:B37,0)),0) - IFERROR(INDEX(C34:C37,MATCH(""Additional Agreement - SPPUA"",B34:B37, 0)),0) - IFERROR(INDEX(C34:C37,MATCH(""Flexible Agreement - FLXT10/20"",B34:B37, 0)),0)"
.Locked = True
End With
Else
With OG.Cells(33, 3)
.Value = "=IF(INDEX(B:B,MATCH(""Total*"",A:A,0))="""",0,INDEX(B:B,MATCH(""*"",A:A,0)))"
.Locked = True
End With
End If
OG.Activate
Cells(Application.WorksheetFunction.Match("Last Month Paid ($)", Range("A:A"), 0), 2).NumberFormat = "$#,##0.00;[Red]$#,##0.00"
OG.Protect ("Password")
Application.ScreenUpdating = True
End Sub
Oh, that brings back memories for me. 哦,那回想起了我。 I think I used to get this error about 10 years ago Excel 2003?
我想我曾经在10年前的Excel 2003上收到过此错误? Maybe?.
也许?。 Excel would get itself into a bit of a state.
Excel会使自己陷入某种状态。 Nothing was wrong with the code, just it would keep coming back with that error.
代码没什么错,只是它将不断返回该错误。
If you save your work close Excel and then reopen, does the error go away? 如果保存工作,请关闭Excel,然后重新打开,该错误是否消失了? If I remember right, it was caused when I called some external API.
如果我没记错的话,那是我调用某些外部API时引起的。 Maybe some other API call in your is causing this error but manifesting at this point... perhaps.
可能是您中的其他一些API调用导致了此错误,但此刻才显现出来……也许。
Sorry but it was 10+ years ago :) 抱歉,但是十年前:)
even if you went through it, you may want to consider the following "restyling" of the code you posted 即使您经历了它,也可能要考虑发布的代码的以下“重新样式化”
Option Explicit
Sub Review()
Dim Cell As Range, rRange As Range
Dim a As String
Dim RR As Worksheet, OG As Worksheet
Set RR = Sheets("Ready for Review")
Set OG = ActiveSheet
OG.Unprotect ("Password")
Set rRange = GetUserInpt(RR)
If rRange Is Nothing Then
MsgBox "You aborted the POLICY selection" _
& vbCrLf & vbCrLf _
& "the procedure ends" _
, vbInformation
Exit Sub
End If
Application.ScreenUpdating = False
OG.Cells(12, 2).Locked = False
For Each Cell In OG.UsedRange
With Cell
If Not .Locked Then
a = OG.Cells(.row, 1)
If Not a = "" Then .value = RR.Cells(CLng(Application.WorksheetFunction.Match(a, RR.Range("A:A"), 0)), _
rRange.Column + .Column - 2)
End If
End With
Next Cell
With OG.Cells(33, 3)
.Locked = False
Select Case Right(OG.Cells(5, 2), 2)
Case "UL", "IL", "PL"
.Formula = "=IF(INDEX(B:B,MATCH(""Total*"",A:A,0))="""",0,INDEX(B:B,MATCH(""Total*"",A:A,0)))-SUM(C34:C37)"
Case "WL"
.Formula = "=IF(INDEX(B:B,MATCH(""Total*"",A:A,0))="""",0,INDEX(B:B,MATCH(""*"",A:A,0))) - IFERROR(INDEX(C34:C37,MATCH(""Additional"",B34:B37, 0)),0) - IFERROR(INDEX(C34:C37,MATCH(""Paid"",B34:B37,0)),0) - IFERROR(INDEX(C34:C37,MATCH(""Additional Agreement - SPPUA"",B34:B37, 0)),0) - IFERROR(INDEX(C34:C37,MATCH(""Flexible Agreement - FLXT10/20"",B34:B37, 0)),0)"
Case Else
.value = "=IF(INDEX(B:B,MATCH(""Total*"",A:A,0))="""",0,INDEX(B:B,MATCH(""*"",A:A,0)))"
End Select
.Locked = True
End With
OG.Activate
Cells(Application.WorksheetFunction.Match("Last Month Paid ($)", Range("A:A"), 0), 2).NumberFormat = "$#,##0.00;[Red]$#,##0.00"
OG.Protect ("Password")
Application.ScreenUpdating = True
End Sub
Function GetUserInpt(sht As Worksheet) As Range
Dim rRange As Range
Application.DisplayAlerts = False
sht.Activate
On Error GoTo InputBoxCanceled
Do While rRange Is Nothing
Set rRange = Application.InputBox(Prompt:="Please select POLICY to review.", _
Title:="SPECIFY POLICY", _
Default:=sht.Cells(3, 1).Address, _
Type:=8)
If rRange.Parent.Name <> sht.Name Then
MsgBox "You must select a cell in '" & sht.Name & "' sheet"
sht.Activate
Set rRange = Nothing
Else
If rRange.row <> 3 And rRange.row <> 17 Then
MsgBox "Value other than a POLICY was selected" _
& vbCrLf & vbCrLf _
& "Select the cell that contains the correct policy number" _
, vbCritical
Set rRange = Nothing
End If
End If
Loop
Set GetUserInpt = rRange
InputBoxCanceled:
On Error GoTo 0
Application.DisplayAlerts = True
End Function
the main revision applies to: 主要修订适用于:
added a GetUserInpt
function to handle the policy selection 添加了
GetUserInpt
函数来处理策略选择
this function: 这个功能:
checks for both the correct selection row and sheet, too (since it's possible the user shifts to another worksheet during selection!) 同时也检查正确的选择行和工作表(因为用户有可能在选择过程中转移到另一个工作表!)
runs a loop until the user selects a proper cell 运行循环,直到用户选择适当的单元格
exits selection upon user canceling the InputBox
, as the only loop escape possibility 用户取消
InputBox
时退出选择,这是唯一的循环转义可能性
made some simplifications here and there, like: 在这里和那里做了一些简化,例如:
eliminated Activate
statements unless really needed 除非确实需要,否则消除了
Activate
语句
reduced the amount of variables to only (nearly) strictly needed ones 将变量的数量减少到(几乎)严格需要的变量
added some With ... End
With blocks to add readability 添加了一些
With ... End
With块以增加可读性
used a Select Case
block instead of an If ... Then ... Else if ... Else ... End if
one, for readability again 使用
Select Case
块而不是If ... Then ... Else if ... Else ... End if
一个,则再次可读
changed .Value
to .Formula
, for a proper syntax 将
.Value
更改为.Formula
,以获取正确的语法
all what above could help you with this project and in future ones, too 以上所有内容都可以帮助您完成本项目,并在以后的项目中也有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.