使用 Exit For 语句可以从 For/Next 循环中退出。以下示例在全局数组名称中搜索名称“Fred”。如果找到了该名称,将返回该名称在数组中的索引。否则返回 1。例如,如果名称数组是:
Array ("Frank", "Helen", "Fred", "Linda")
则公式返回 3。
Global names () As String
'The names array has been initialized and filled
'in other formulas
Dim i
formula = -1
'The UBound function returns the size of its array
'argument
For i = 1 to UBound (names)
If names (i) = "Fred" Then
formula = i
Exit For
End If
Next I