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