When is a number not a number?

ACommandLineKindaGuy

Active Member
Joined
May 11, 2002
Messages
378
Office Version
  1. 365
Platform
  1. Windows
I have the following code to check the input of a Textbox.

So, for say TextBox301, I call a routine to validate the number I enter. I have a lot of groups of TextBoxes and they all have different maximum integer values, so I pass the maximum value using public constant n:

<code>Private Sub TextBox301_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

Call ValidateInput(TextBox301, 5)

End Sub

Private Sub ValidateInput(MyControl, n) 'n = Control's max value

'#UNDER DEVELOPMENT############### THE PROBLEM IS THE .Value > n STATEMENT
'We only have to check for integers since the maximum characters is 1...
With MyControl
If Not IsNumeric(.Value) Or .Value < 1 Or .Value > n Then
MsgBox "Must be numeric, or an integer between 1 and " & n & "... ", vbCritical, "Invalid Entry:"
.SelStart = 0
.SelLength = .MaxLength + 1
Cancel = True
End If
End With
'#END UNDER DEVELOPMENT###########
End Sub</code>

The problem is with the ".Value > n" portion of the If statement. If I remove it, the If statement doesn't fire. If I substitute ".Value * 1 > n" it also doesn't fire, but then of course the routine errors out when I enter a letter.

So, anyone have any idea WHAT is going on? If I had any hair, I'd be bald...
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
'We only have to check for integers since the maximum characters is 1...
With MyControl
If Not IsNumeric(.Value) Or .Value < 1 Or .Value > n Then
If the value of 'n' is always a single digit number, you can use this If..Then test for it...

If .Value Like "[1-" & n & "]" Then
 
Upvote 0
Thanks--works great. But, I still don't understand why we have to use this workaround...
This was your line of code...

If Not IsNumeric(.Value) Or .Value < 1 Or .Value > n Then

I think a problem occurs when .Value is not a number... I believe the test for being less than or greater than a number will fail and raise a "Type mismatch" error. If you wanted to stick with your logic, I think you would need to do your tests something like this...
Code:
If Not IsNumeric(.Value) Then
  ' Display your error warning here
ElseIf .Value < 1 Or .Value > n Then
  ' Display your error warning here
Else
  ' Everything is okay so put your continuing code here
End If
 
Upvote 0
Rory,

LOL, well that makes sense: a TextBox contains text

Rick,

Given Rory's astute observation, seems that an easier way for be to simply use VAL(.Value)...

Thanks again guys
 
Upvote 0
FYI, I removed my earlier post as my comment about > not interpreting numbers was not correct.

Val is fine as long as you don't have international users to worry about, or use any currency formatting in the textboxes.
 
Upvote 0
Rick,

Given Rory's astute observation, seems that an easier way for be to simply use VAL(.Value)...
Yes, as long as your TextBox is set to receive a maximum of one character. I would note, however, that you will need to do two comparisons (And'ed together), one to make sure the value is not 0 and the other to make sure it is less than or equal to n whereas my suggestion only requires the single Like comparison.
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

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