1

I can't get the While/Wend loop to work at all. Even the tiniest possible case, such as below:

Sub TestingWhile()

Dim i As Integer

i = 0
Do While i < 10
   i = i + 1
Wend

End Sub

The result of that code is an error message window saying "Compile error: Wend without While".

I found people having this error, but only because they always had some wrong if/else statements. This obviously is not the case. What's going on?

By the way, I'm using Microsoft Visual Basic for Applications 7.1 while in Excel 2013.

EDIT - Question solved. Syntax, beginner mistake: Do While is closed by Loop, and While is closed by Wend

  • @ SJR: if this is serious, please point to such page. I've been looking in Google for the last hour or so. @R3uK: No, I am not trying to break out of the loop. I just want the loop to work in the first place. Right now the code is not running. –  Mar 17 '17 at 14:47
  • I googled "excel while wend" and the first result explains the syntax of a While-Wend loop. – SJR Mar 17 '17 at 14:51
  • I followed the syntax. There is nothing wrong with it, apparently. I even quoted (in another comment here) what is probably that page you are talking about (). Did you bother to even read my question or the code? –  Mar 17 '17 at 15:00
  • 2
    And yet you still left a Do in there? – SJR Mar 17 '17 at 15:10
  • Ok, now it works. Went over my head all this time. Funny though, how you could have pointed that out in the first comment, or could have ignored the question or downvoted, but chose instead to waste all of our times while creating a bad experience for someone else. What a great website to ask questions. –  Mar 17 '17 at 15:13
  • proper way to mark a question as solved is by accepting one of the answers :) Cheers, beginner's mistake, we've all been there at one time or another. – David Zemens Mar 17 '17 at 15:26
  • SJR and other bedroom heroes get off on sneering condescension – Delete Me Mar 17 '17 at 16:37

1 Answers1

1

Do While is closed by Loop and While is closed by Wend :

Sub TestingWhile()

Dim i As Integer

i = 0
Do While i < 10
   i = i + 1
Loop

End Sub
R3uK
  • 14,417
  • 7
  • 43
  • 77
  • It works when closing with the "Loop", but why is it then that a lot of code uses While/Wend w/o problems? E.g. the solution in . Also, many popular pages still point to While/Wend as the way to make the While loop in VBA, like . The reason why I wanted to use the While/Wend instead of the While/Loop was because I had problems in a big code that used While/Loop and was trying to debug it, and I thought the way the While was written was the problem. –  Mar 17 '17 at 14:54
  • Ok, the problem was that "Do While" is closed by "Loop", and "While" is closed by "Wend". Beginner's mistake. –  Mar 17 '17 at 15:21
  • 1
    @fightmebro : Yup, sorry I should have been clearer in my answer, I've edited it to be more accurate! ;) – R3uK Mar 17 '17 at 15:24