【发布时间】:2016-01-12 16:15:11
【问题描述】:
在我的 VBA 项目中,我偶尔会弹出 MsgBox 以通知用户在子例程运行后某些内容已“完成”或“更新”。 如果没有 MsgBox,它似乎运行良好,但插入一个似乎给我一个错误。 不确定是否有必要在此处显示整个代码,因为它非常大,但在子例程结束时我只想...
MsgBox ("Completed")
后面是End Sub
但是,当我运行它然后单击 Msgbox 上的 OK 时,我收到一个运行时错误,单击 DeBug 时,它会突出显示 End Sub。
有什么理由会引发这样的错误吗? 我错过了什么吗? 非常感谢
这里有一些代码
'Add unique data to new location
For i = 1 To UnqArray1.Count
rCell(i, 1) = UnqArray1(i)
Next
'Move Split Array into a new array
Set rTable2 = rCell
rng2() = rTable2.Value
'Filter into unique items
On Error Resume Next
For Each b In rng2
UnqArray2.Add b, b
Next
'Clear location
rCell.Clear
'Add new array to location
For i = 1 To UnqArray2.Count
rCell(i, 1) = UnqArray2(i)
Next
'Find the end of the category list
lastrow = Worksheets("CatMatch").Range("Q100000").End(xlUp).Row
'Sort alphabetically
Worksheets("CatMatch").Range("Q1:Q" & lastrow).Sort key1:=Range("Q1"), order1:=xlAscending, Header:=xlNo
'Copy it to CatMatch
Worksheets("CatMatch").Range("Q1:Q" & lastrow).Copy Destination:=Worksheets("CatMatch").Range("B15")
MsgBox "Completed"
End Sub
【问题讨论】:
-
这取决于消息框在代码中的位置。也许发布更多代码,这样我们就可以看到发生了什么!注意:将代码发布在您的原始问题中,而不是在评论中。也许最后 10 - 20 行代码就足够了??
-
错误是什么是?
-
它应该是
MsgBox "Completed"(没有括号),因为您没有使用MsgBox的返回值——尽管这并不能解释为什么您的代码会抛出错误。跨度> -
我试过不带括号。错误是“代码执行已中断”。当我单击 Debug 时,它会突出显示 End Sub。我在原始问题中添加了一些代码。谢谢大家的cmets。
-
不用
On Error Resume Next试试。这条线应该很少使用。这使得调试非常困难。