“虽然. .当“VBA不工作?”

时间:2022-07-05 21:21:25

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 MSDN this should work but it doesn't. What has happened?

代码不能编译。在代码之上,我只有声明。根据MSDN,这应该起作用,但它没有。发生了什么?

2 个解决方案

#1


52  

While constructs are terminated not with an End While but with a Wend.

当构造终止时,不是用End而是用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 to deals 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帮助文件中:

Tip The Do...Loop statement provides a more structured and flexible way to perform looping.

使做……循环语句提供了一种更结构化和更灵活的方法来执行循环。

#2


14  

VBA is not VB/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:

正确的使用方法是“做”。循环语句(VBA)。还可以查看Excel VBA的文章,Do While和Do Until。一种写法是:

Do While counter < 20
    counter = counter + 1
Loop

(But a For..Next might be more appropriate here.)

(但一个. .下一个可能更合适)

Happy coding.

快乐的编码。

#1


52  

While constructs are terminated not with an End While but with a Wend.

当构造终止时,不是用End而是用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 to deals 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帮助文件中:

Tip The Do...Loop statement provides a more structured and flexible way to perform looping.

使做……循环语句提供了一种更结构化和更灵活的方法来执行循环。

#2


14  

VBA is not VB/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:

正确的使用方法是“做”。循环语句(VBA)。还可以查看Excel VBA的文章,Do While和Do Until。一种写法是:

Do While counter < 20
    counter = counter + 1
Loop

(But a For..Next might be more appropriate here.)

(但一个. .下一个可能更合适)

Happy coding.

快乐的编码。