“While .. End While” doesn't work in VBA?

前端 未结 2 385
北荒
北荒 2020-12-16 09:56

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
           


        
相关标签:
2条回答
  • 2020-12-16 10:30

    While constructs are terminated not with an End While but with 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 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.

    Also in the VBA help file:

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

    0 讨论(0)
  • 2020-12-16 10:47

    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.

    0 讨论(0)
提交回复
热议问题