Noelia Sancho Mendez
Noelia Sancho Mendez

Reputation: 151

Enters an infinite loop

I'm new at vba so I'm starting with examples I find and things like that.

Now I'm trying to do an example i found on internet which is about a game where you have to guess a random number.

My game is based on 3 columns and 2 buttons.

In 1st column "Nº introducido" I want that appears the number that user introduced in an inputbox,

in 2nd column "Situación" I want that programm tells teh user if the random number is greater, smaller or equal to the number he introduced

and the 3st column "Intentos" must appears the number of attemps the user needed.

All this must happen when I press the "Jugar" button but this enters an infinite loop, I even commented blocks of code to know where was the problem and I cant find it. The most extrange is I'm using the same code than in the example.

image

This is my piece of code:

Private Sub CommandButton2_Click()

    Dim aleatorio, minum, fila As Integer

    aleatorio = Math.Round(Math.Rnd * 100)
    Debug.Print aleatorio

    While aleatorio <> minum
        minum = InputBox("Introduce un nº entre 1 y 100", "", , 150, 150)

'        Range("c22").Offset(fila, 0).Value = minum
'
'        If aleatorio > minum Then
'            MsgBox "El nº es más alto"
'        ElseIf aleatorio < minum Then
'            MsgBox "El nº es más bajo"
'        Else
'            MsgBox "Correcto"
'        End If
    Wend
End Sub

I'm using a debug.print to know the random number and I'm trying to put the same number that appears but the inputbox never is closed.

Upvotes: 2

Views: 90

Answers (2)

JvdV
JvdV

Reputation: 75980

When you don't define variable, VBA will try to do so for you. So aleatorio = Math.Round(Math.Rnd * 100) will pass as a Variant/Single variable while InputBox("Introduce un nº entre 1 y 100", "", , 150, 150) will pass as a Variant/String variable, see "watches" below:

enter image description here

So the infinite loop comes from comparing a numeric value against a string value e.g.: 71 vs "71" which would never be the same. To change that you could either transform your minim variable into a numeric value, e.g.:

minum = CDbl(InputBox("Introduce un nº entre 1 y 100", "", , 150, 150))

Or:

minum  = Application.InputBox("Introduce un nº entre 1 y 100", "", , 150, 150, Type:=2)

Or even better declare variables:

Dim aleatorio As Double, minum As Double, fila as Double

Upvotes: 4

TheJeebo
TheJeebo

Reputation: 166

Try defining all the variables as integers, the first time through the minum variable was reading as a string and was never equaling the integer variable, I tried this one and it worked.

Private Sub CommandButton2_Click()

Dim aleatorio As Integer, minum As Integer, fila As Integer

aleatorio = Math.Round(Math.Rnd * 100)
Debug.Print aleatorio

While aleatorio <> minum
    minum = InputBox("Introduce un nº entre 1 y 100", "", , 150, 150)

    Range("c22").Offset(fila, 0).Value = minum

    If aleatorio > minum Then
        MsgBox "El nº es más alto"
    ElseIf aleatorio < minum Then
        MsgBox "El nº es más bajo"
    Else
        MsgBox "Correcto"
    End If
Wend
End Sub

Upvotes: 3

Related Questions