vba:每个循环只做第一件事

for each element in sarray if ... then if this is the first time the above IF statenent was executed ... then end if next element 

我只想第一次执行第一个IF THEN语句。 我的逻辑有很多问题。 请帮忙!

 var isExecuted = false for each element in sarray if ... then if Not isExecuted Then --do stuff isExecuted = true end if end if next element 

这样做可能有更好的方法,但是你可以有一个布尔variablesfirst_time,它将在if语句中作为true开始并设置为false。 然后你可以检查

 If first_time = True Then Stuff End If 

如果我正确地理解了你,你有N个元素,并且你只想为第一个元素执行一些操作。 所以像你想做的那样做是浪费处理器时间。

所以我build议不用引入新的布尔variables来重写你的逻辑。 它更干净,更快。 像这样的东西,例如:

 Dim intCount As Integer If (some condition) Then DoSomething with sarray(0) For intCount = 1 To sarray.Length //Do something Next intCount 
 flag = true For each ... if flag do whatever with first element flag = false endif do what you want with all elements ... including the first next