ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   User Input Of A Number (https://www.excelbanter.com/excel-programming/316534-user-input-number.html)

Chuckles123[_49_]

User Input Of A Number
 

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

--
Chuckles12
-----------------------------------------------------------------------
Chuckles123's Profile: http://www.excelforum.com/member.php...fo&userid=1494
View this thread: http://www.excelforum.com/showthread.php?threadid=27517


Dave Peterson[_5_]

User Input Of A Number
 
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.





Chuckles123 wrote:

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

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

Thanks for a response,
Chuckles123

--
Chuckles123
------------------------------------------------------------------------
Chuckles123's Profile: http://www.excelforum.com/member.php...o&userid=14948
View this thread: http://www.excelforum.com/showthread...hreadid=275174


--

Dave Peterson


All times are GMT +1. The time now is 06:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com