User Input Of A Number

  • Thread starter Thread starter Chuckles123
  • Start date Start date
C

Chuckles123

Why are there two "If"s, two "Then"s, zero "Else"s, and zero "En
If"s???
(assuming all of that counts as one question)

And how do I modify Ivan's solution to display a short error messag
that a two or three digit number has NOT been input by user?

Thanks for a response,
Chuckles12
 
You could do something like:

Option Explicit
Sub Tester()
Dim Result

Again:
Result = Application.InputBox("Enter 2 or 3 digit number", Type:=1)
If Result = False Then Exit Sub
If Len(Result) > 3 Or Len(Result) < 2 Then
MsgBox "Not a 2 or 3 digit number"
GoTo Again
End If

MsgBox Result

End Sub

Or maybe just put it in the message that's displayed:

Option Explicit
Sub Tester2()
Dim Result
Dim msg As String

msg = "Enter 2 or 3 digit number"

Again:
Result = Application.InputBox(msg, Type:=1)
If Result = False Then Exit Sub
If Len(Result) > 3 Or Len(Result) < 2 Then
msg = "That's not correct" & vbLf & msg
GoTo Again
End If

MsgBox Result

End Sub

There are two types of If statements.

The first type is on one logical line (maybe on multiple physical lines if you
use the continuation character (space underscore)

If condition Then [statements] [Else elsestatements]

The second is the block form:

If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements] ...
[Else
[elsestatements]]
End If

(I stole the examples from VBA's help.)

And with block if's, you don't always need the Else portion:

if someconditionistrue then
msgbox "good job
end if

I sometimes put the "else" portion, but just have a comment.

If somecondtionistrue then
msgbox "good job"
else
'do nothing
end if

But that's just a personal preference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top