Loop do while in vba

2

I have made a module that according to what number you enter with the conditional if, I get a message or another.

I have created a repetition with the loop do ... loop, so that I only introduce from 1 to three and if I do not ask for it again, but it does not come out of the loop, it asks me on each turn to enter the data. put 1 or 2 or 3

The variable I declare as Variant or as String and it does not matter, because the inputbox gives as a string and not as Integer.

I'm looking for information on how it's used, but I think it's like that, I do not see the problem, can you help me?

I put the code:

Sub Escalado()

Dim Escalado As Variant

Do While Escalado <> "1" Or "2" Or "3"
MsgBox "Tienes que poner un numero del 1 al 3", vbInformation, "ENVIO CORREO PLANTILLA"
Escalado = InputBox("Tipo de escalado 1, 2 o 3", "ENVIO PLANTILLA", " SOLO INTRODUCIR UN NUMERO 1 - 2 o 3")

Loop

If Escalado = "1" Then
MsgBox "escalado n1"

ElseIf Escalado = "2" Then
MsgBox "escalado n2"

Else   
MsgBox "escalado n3"

End If


End Sub
    
asked by luis b 22.08.2017 в 17:33
source

2 answers

1

your code has several problems ...

First and foremost

Escalado <> "1" Or "2" Or "3"

this translates as Scaling other than 1 O 2 O 3, and if you look at the debuguer it returns 3. because I really do not know how VBA is understanding it, but there is no way to make a difference to 1 or 2 or 3 of that form .. what you want is written as follows:

(Escalado <> "1") Or (Escalado <> "2") Or (Escalado <> "3")

But nevertheless, you put this in a while, so, if you climb it is 1 (and you want it to come out) it will give you the first one .. but the other two give it true ...

then you really should do something like this:

(Escalado <> "1") And (Escalado <> "2") And (Escalado <> "3")

Because what you are looking for is that the 3 fail ... if one does not fail, it will give false and leave the loop.

    
answered by 22.08.2017 / 17:53
source
0

As I see in the code, you are declaring the variable as soon as the procedure starts, this encerates the variable so that some value would never be equal.

Try this code:

Dim miEscalado As String
Dim estado As Boolean

Sub Escalado()
    estado = False
    Do While estado = False
        Call Mensaje
    Loop

    If miEscalado = "1" Then
        MsgBox "escalado n1"
    ElseIf miEscalado = "2" Then
        MsgBox "escalado n2"
    Else
        MsgBox "escalado n3"
    End If
End Sub

Sub Mensaje()
    estado = False
    MsgBox "Tienes que poner un numero del 1 al 3", vbInformation, "ENVIO CORREO PLANTILLA"
    miEscalado = InputBox("Tipo de escalado 1, 2 o 3", "ENVIO PLANTILLA", " SOLO INTRODUCIR UN NUMERO 1 - 2 o 3")
    If miEscalado = "1" Or miEscalado = "2" Or miEscalado = "3" Then
        estado = True
    End If
End Sub
    
answered by 22.08.2017 в 18:19