VBA Loop until certain value is entered (to infinity) with For Loop

mamontenok

New Member
Joined
Mar 17, 2016
Messages
2
Hi there,

Trying to come up with a code that will loop an infinite number of times until the user enters a specific range of values. I can do it using 'Do While' or 'Do Until' loops but the assignment specifically mentions 'For Loop'. Please help. Thanks!

Here is what I have now:

Option Explicit
Sub Problem3()


Dim i As Integer, n As Integer, num As Integer
n = 1000


For i = 0 To n
num = InputBox("Enter number")
If (num <= 20 And num >= 10) Then
MsgBox "Thank you for ending this loop"
Exit For
End If
Next i
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi,

I don't really like to do homework because it kind of defeats the purpose, but it does look like you at least gave it a shot.

You obviously know that the code does run as written, except it is going only to loop for 1000 times, or when i = n or 1000. What if i never reached n. You really don't even need n, you could just put your 1000 in n's place.

Also, although it works your If statement is a drop unorthodox.

Additionally, you made no provision for the case where the user clicks cancel on your Msgbox, when that happens the code barfs, but that is another lesson.

How about this:

Code:
Sub Problem3()

    Dim i As Integer, num As Integer


    For i = 0 To 1      'I did away with "n" because it served no purpose
        num = InputBox("Enter number")
            If num <= 20 And num >= 10 Then
                MsgBox "Thank you for ending this loop"
                Exit For
            End If
        i = 0   ' If you keep i at 0 it will never be greater than 1 and end the For/Next loop
    Next i

End Sub

Good luck. I hope this helps.

igold
 
Last edited:
Upvote 0
That is (for me at least) one of the fun parts of writing macros... Namely the problem solving or figuring out the logic of how something can be accomplished.

At any rate, I am glad I could help and thank you very much for the feedback!

Regards,

igold
 
Upvote 0

Forum statistics

Threads
1,215,329
Messages
6,124,301
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top