[vba] "While .. End While" doesn't work in VBA?

The code below is VBA for Excel. I am using the Visual Basic editor that comes with Excel 2007.

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?

This question is related to vba excel

The answer is


VBA is not 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 While counter < 20
    counter = counter + 1
Loop

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

Happy coding.