我设置了一系列VBA集合,我将单元格值与集合中的值进行比较,但有时集合项目不存在,是否有一个函数可以帮助此代码在其中一个项目中运行未定义:
Select Case Range("A" & ActiveCell.row).Value
Case Is = item1.Item(1)
item1.Add ActiveCell.Value
Case Is = item2.Item(1)
item2.Add ActiveCell.Value
Case Is = item3.Item(1)
item3.Add ActiveCell.Value
Case Is = item4.Item(1)
item4.Add ActiveCell.Value
Case Is = item5.Item(1)
item5.Add ActiveCell.Value
End Select
在这种情况下,item5.Item(1)未定义。
答案 0 :(得分:0)
使用错误处理程序,这是最简单的处理程序:
On Error GoTo Handler
Select Case Range("A" & ActiveCell.Row).Value
Case Is = item1.Item(1)
item1.Add ActiveCell.Value
Case Is = item2.Item(1)
item2.Add ActiveCell.Value
Case Is = item3.Item(1)
item3.Add ActiveCell.Value
Case Is = item4.Item(1)
item4.Add ActiveCell.Value
Case Is = item5.Item(1)
item5.Add ActiveCell.Value
End Select
Handler:
MsgBox "Item not found"
Exit Sub 'Function