“While .. End While”在 VBA 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9733699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"While .. End While" doesn't work in VBA?
提问by Gorgen
The code below is VBA for Excel. I am using the Visual Basic editor that comes with Excel 2007.
下面的代码是 Excel 的 VBA。我正在使用 Excel 2007 附带的 Visual Basic 编辑器。
Dim counter As Integer
counter = 1
While counter < 20
counter = counter + 1
end while '<- the compiler is complaining about this statement
The code doesn't compile. Above the code I have only declarations. According to MSDNthis should work but it doesn't. What has happened?
代码无法编译。在代码上方我只有声明。根据MSDN,这应该有效,但没有。发生了什么事?
回答by Jean-Fran?ois Corbett
While
constructs are terminated not with an End While
but with a Wend
.
While
结构不是用 anEnd While
而是用 a终止的Wend
。
While counter < 20
counter = counter + 1
Wend
Note that this information is readily available in the documentation; just press F1. The page you link todeals with Visual Basic .NET, not VBA. While (no pun intended) there is some degree of overlap in syntax between VBA and VB.NET, one can't just assume that the documentation for the one can be applied directly to the other.
请注意,此信息在文档中很容易获得;只需按F1。您链接到的页面涉及 Visual Basic .NET,而不是 VBA。虽然(不是双关语)在 VBA 和 VB.NET 之间的语法有一定程度的重叠,但不能仅仅假设一个的文档可以直接应用于另一个。
Also in the VBA help file:
同样在 VBA 帮助文件中:
TipThe
Do...Loop
statement provides a more structured and flexible way to perform looping.
提示该
Do...Loop
语句提供了一种更加结构化和灵活的方式来执行循环。
回答by Gorgen
VBAis notVB/VB.NET
VBA是不是VB / VB.NET
The correct reference to use is Do..Loop Statement (VBA). Also see the article Excel VBA For, Do While, and Do Until. One way to write this is:
正确的使用参考是Do..Loop Statement (VBA)。另请参阅Excel VBA For、Do While 和 Do until 一文。一种写法是:
Do While counter < 20
counter = counter + 1
Loop
(But a For..Nextmight be more appropriate here.)
(但是For..Next在这里可能更合适。)
Happy coding.
快乐编码。