vba - "While .. End While"在 VBA 中不起作用?

标签 vba excel

下面的代码是 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

代码无法编译。在代码上方我只有声明。根据MSDN这应该有效,但事实并非如此。发生了什么?

最佳答案

While 构造不是用 End While 终止的,而是用 Wend 终止的。

While counter < 20
    counter = counter + 1
Wend

请注意,此信息可以在文档中轻松获得;只需按F1即可。 page you link to处理 Visual Basic .NET,而不是 VBA。虽然(没有双关语)VBA 和 VB.NET 之间的语法存在一定程度的重叠,但不能仅仅假设其中一个的文档可以直接应用于另一个。

另外在 VBA 帮助文件中:

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

关于vba - "While .. End While"在 VBA 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9733699/

相关文章:

Excel VBA 打印卡住

excel - 如何确定单元格是否包含非西方文本? (Excel)

vba - 使用 VBA 隐藏数据透视表

excel - 循环遍历文件扩展名,excel vba

excel - 如何在 Excel VBA 中对字符串进行 URL 编码?

excel - 如何避免excel,SSRS报告中较粗的边框宽度?

events - Worksheet_Change 永远不会触发

algorithm - Excel多项式曲线拟合算法

vba - VBA 声明波动性的工作原理

excel - 如何使用excel中的一列作为另一列中的搜索参数以仅输出某些字符串