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
语句时执行某些操作。我的逻辑有很多问题。请帮忙!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
语句时执行某些操作。我的逻辑有很多问题。请帮忙!vba:对每个循环只做第一个操作
var isExecuted = false
for each element in sarray
if ... then
if Not isExecuted Then
--do stuff
isExecuted = true
end if
end if
next element
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
有可能是这样做的更好的办法,但你可以有一个布尔变量FIRST_TIME将开始时为真实,得到的if语句设置为false。然后,您可以检查
If first_time = True Then
Stuff
End If
那不是vba,是吗? – 2010-11-19 19:13:25
它更像是伪代码,虽然逻辑相同...... – user472875 2010-11-19 19:14:21
@belisarius:事实上,这是迄今为止这个问题中唯一的VBA代码。你不会编译。也不会@ CDeutsch的。 – GSerg 2010-11-19 19:17:55
如果我理解正确的话,你有N个元素,并且要只对第一个元素进行一些操作。所以像你想做的那样做就是浪费处理器时间。
所以我会建议重写你的逻辑而不引入新的布尔变量。它更干净,更快。例如:
Dim intCount As Integer
If (some condition) Then
DoSomething with sarray(0)
For intCount = 1 To sarray.Length
//Do something
Next intCount
它不是数组的第一个对象,它是第一个满足条件的对象。如果sarray(5)是If条件为真的第一个条件,该怎么办? – user472875 2010-11-19 19:24:56
你认为它是满足条件的第一个元素,但说实话,如果...之间可以放置任何东西,并且由于用户没有说出真正的条件,我认为我的答案保持有效。 – Wodzu 2010-11-19 19:31:03
如果这是所有循环必须做的事情,那么添加'Exit For'作为do stuff例程的最后一行。 – ForEachLoop 2010-11-19 20:26:27