ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   InputBox (https://www.excelbanter.com/excel-programming/412703-inputbox.html)

James8309

InputBox
 
Hi, All

I have a macro which starts with InputBox. My Macro will only run
correctly without an error if and only if the user has typed the right
number of letters or numbers into the inputbox. PROBLEM IS........
even though I put Note on the inputbox saying "Check your digits, if
it is more than 10digits it will make an error" they still manage to
screw things up. sigh..

How do I lock the inputbox in such way that unless input is 'n' number
of digits exactly, it wouldn't even run the macro.

Do i use 'if' ?


orksheets("RU").Range("A1") = Application.InputBox("Type Cost Centre
Number, i.e.

Nayab

InputBox
 
On Jun 17, 10:14*am, James8309 wrote:
Hi, All

I have a macro which starts with InputBox. My Macro will only run
correctly without an error if and only if the user has typed the right
number of letters or numbers into the inputbox. PROBLEM IS........
even though I put Note on the inputbox saying "Check your digits, if
it is more than 10digits it will make an error" *they still manage to
screw things up. sigh..

How do I lock the inputbox in such way that unless input is 'n' number
of digits exactly, it wouldn't even run the macro.

Do i use 'if' ?

orksheets("RU").Range("A1") = Application.InputBox("Type Cost Centre
Number, i.e.


In caase u need the input length to be made more than or equal to 10
and you do not want to proceed till the correct length is entered, you
can use sth like:

Sub read_input()
Dim value As String
Do While Len(value) < 10
value = Application.InputBox("Type Cost Centre number:")
Loop
Range("A1").value = value
End Sub

Harald Staff[_2_]

InputBox
 
What you must do is validate the input BEFORE putting it into the vulnerable
cell. This code rrequires 10 digits exactly:

Sub Test()
Dim S As String
Dim OK As Boolean
Do
OK = True
S = InputBox("10 digits please:", , S)
If StrPtr(S) = 0 Then Exit Sub 'cancelled
If Not S Like "##########" Then OK = False '10 digits
Loop Until OK = True
Worksheets("RU").Range("A1") = S
End Sub

HTH. Best wishes Harald

"James8309" wrote in message
...
Hi, All

I have a macro which starts with InputBox. My Macro will only run
correctly without an error if and only if the user has typed the right
number of letters or numbers into the inputbox. PROBLEM IS........
even though I put Note on the inputbox saying "Check your digits, if
it is more than 10digits it will make an error" they still manage to
screw things up. sigh..

How do I lock the inputbox in such way that unless input is 'n' number
of digits exactly, it wouldn't even run the macro.

Do i use 'if' ?


orksheets("RU").Range("A1") = Application.InputBox("Type Cost Centre
Number, i.e.




All times are GMT +1. The time now is 10:54 AM.

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