vba : do something only on the first for each loop
for each element in sarray
if ... then
if this is the first time the above IF statenent was executed ... then
end if
next element
i want to do something ONLY the first time that the first IF THEN
state开发者_运维技巧ment is executed. i am having a lot of problems with my logic. please help!
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
There is probably a better way of doing this but you can have a boolean variable first_time which will start out as true and get set to false in the if statement. You can then check
If first_time = True Then
Stuff
End If
If I understand you correctly you have N elements, and you want to perform some operation only for the first element. So doing this like you want to do is waste of processor time.
So I would suggest to rewrite your logic without introducing a new boolean variable. It's cleaner and faster. Something like this for example:
Dim intCount As Integer
If (some condition) Then
DoSomething with sarray(0)
For intCount = 1 To sarray.Length
//Do something
Next intCount
精彩评论