Fighter Jet
Fighter Jet

Reputation: 407

Putting loop inside a Select Case generates error

I have put a do/while statement inside a SELECT CASE/END SELECT and it generates the error

Case without Select Case.

Select Case myVariable
    Case 0
        Do
        While ...
    Case 1
    ...
End Select

Upvotes: 1

Views: 598

Answers (3)

braX
braX

Reputation: 11755

That's not how Do While loops work. Is has nothing to do with your Select Case

try it like this:

Sub test()
  Dim x As Integer
  Dim y As Integer

  y = 0

  Select Case y
    Case 0
      Do While x < 10
        x = x + 1
      Loop
      MsgBox x
    Case 1
      MsgBox "end"
  End Select

End Sub

or

Sub test()
  Dim x As Integer
  Dim y As Integer

  y = 0

  Select Case y
    Case 0
      Do 
        x = x + 1
      Loop While x < 10
      MsgBox x
    Case 1
      MsgBox "end"
  End Select

End Sub

Do Loops

Upvotes: 3

Mesut Akcan
Mesut Akcan

Reputation: 897

another alternative use

Select Case myVariable
    Case 0
        Dim Counter
        Counter = 0 ' Initialize variable.
        While Counter < 20 ' Test value of Counter.
             Counter = Counter + 1 ' Increment Counter.
        Wend ' End While loop when Counter > 19.
        Debug.Print Counter ' Prints 20 in the Immediate window.
    Case 1
    ...
End Select

Upvotes: 0

Naveen Kumar
Naveen Kumar

Reputation: 2006

Try this:

Sub Test()
Dim myvariable As Integer
myvariable = 0

Select Case myvariable
   Case Is = 0
        i = 0
         Do
              MsgBox i
              i = i + 1
         Loop While (i < 10)
  Case Else
      MsgBox "Else"
End Select
End Sub

Upvotes: 1

Related Questions