By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,117 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,117 IT Pros & Developers. It's quick & easy.

Simple If/Then/else Question

P: 1
I'm trying to do a very simple if, then, else program in excel VBA (which I'ved simplified even further below). What I'm hoping will happen is that it will keep asking me for a variable until its greater than 5 and then it will post a single message of 'finished'
What actually happens is that depending on how many time I provide an input before I get to something that's greater than 5 it will then repeat the 'finished' message that number of times. What am I doing wrong.

Sub test()
Dim var1 As Variant

var1 = InputBox("variable please")
If var1 < 5 Then
Call test
Else
MsgBox ("two")
End If

MsgBox ("finished")
End Sub
Jun 26 '08 #1
Share this Question
Share on Google+
1 Reply


kadghar
Expert 100+
P: 1,295
Thats because the msgbox is shown once for everytime you call Test, because it always run up to the last line.

Imagine you enter the number > 5 in the 2nd attemp. then the first atemp will call Test again, in the second one, Test will finish and show "Finished", then it'll go back to finish the Test you started first, and i'll do so

Msgbox("two") will show only once ^.^
that'll give you an idea.

what i recomend you to do here is not to use a recursion formula. A single loop is enough:

Expand|Select|Wrap|Line Numbers
  1. sub test()
  2. dim var1
  3. do
  4.     var1=inputbox("variable please")
  5. loop until var1 > 5
  6. msgbox "two"
  7. msgbox "finish"
  8. end sub
HTH
Jun 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.