VBA:为什么人们将变量名称包含在“Next”声明? [英] VBA: Why do people include the variable's name in a "Next" statement?
问题描述
For foo = 1 to 10
'do但是,当我在线阅读代码片段时,人们总是这样做:
/ p>
对于foo = 1到10
'做某事
下一个foo
我没有注意到这两者之间有任何区别,而且在 next
语句更可取。这两者之间有什么区别(如果有的话)?
p>如果您在 Next 语句中省略 counter ,则继续执行,就好像包含计数器一样。如果在其相应的 For 语句之前遇到 Next 语句,则会发生错误。
人们之所以还添加计数器来增加可读性。
I have always written my For-loops like this:
For foo = 1 to 10
' do something
Next
However, when I read code snippets online, people always do this:
For foo = 1 to 10
' do something
Next foo
I have not noticed any difference between the two, and I can't find any documentation on next
statement is more desirable. What is the difference between those two (if any)?
The counter after the Next
statement is optional. It used to be required in BASIC-derived languages, but this is no longer the case in VBA.
You can check the VBA reference:
If you omit counter in a Next statement, execution continues as if counter is included. If a Next statement is encountered before its corresponding For statement, an error occurs.
The reason people still add the counter it to increase readability.
这篇关于VBA:为什么人们将变量名称包含在“Next”声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!