IsNumeric

Sherrise

New Member
Joined
Oct 25, 2021
Messages
18
Office Version
  1. 365
Platform
  1. Windows
I have a TxtBox with a number in it. I want to have a validation. If blank a message comes up, if it is not a numeric number a message comes up. The first If statement works, but not the IsNumeric!
2014-151566

This is my coding. What am I doing wrong?

If Me.TxtCaseNumber.Value = "" Then
MsgBox "This field must be field in with a numeric #", vbCritical
Exit Sub
End If


If IsNumeric(Me.TxtCaseNumber & "e-0".Value) = False Then
MsgBox "Please enter a Numeric Number", vbCritical
Exit Sub
End If
 
So does a valid result always have a 4 digit year followed by a dash then another number

eg 2023-0001

so 20230001 would be invalid? You need to tell us what you want to check for.
 
Upvote 0

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.
The will always be be a dash after the first for numbers.
There could be a M in front of the first for numbers. It will never be a different Letter.
This would be valid result.
####-###### 2023-459489
or
M####-#### M2023-459489

Correct this would be in valid 20230001
Correct this would be in valid M20230001
 
Upvote 0
Try this:

VBA Code:
x = Replace(UCase(Me.TxtCaseNumber), "M", "")
If x Like "####-*" Then
    If IsNumeric(Split(x, "-")(1)) Then
        MsgBox "Please enter a Numeric Number", vbCritical
        Exit Sub
    End If
End If
 
Upvote 0
I got a run-time error 13; Type mismatch
This part of the coding was highlighted when debug

x = Replace(UCase(Me.TxtCaseNumber), "M", "")
 
Upvote 0
Hello. You can try the following:

VBA Code:
TxtCaseNumber = UCase(TxtCaseNumber)
With CreateObject("VbScript.RegExp")
  .Global = True: .Pattern = "M?\d{4}-\d{1,}": Set mTest = .Execute(TxtCaseNumber)
End With
If mTest.Count = 0 Then MsgBox "Please enter a Numeric Number", vbCritical: Exit Sub
If mTest(0) <> TxtCaseNumber Then MsgBox "Please enter a Numeric Number", vbCritical: Exit Sub
 
Last edited:
Upvote 0
Another regexp option that you could try

VBA Code:
With CreateObject("VBScript.RegExp")
  .Pattern = "^M?\d{4}-\d+$"
  If Not .Test(Me.TxtCaseNumber.Value) Then MsgBox "Please enter a valid case number"
End With
 
Upvote 0
BTW I would definitely avoid using IsNumeric for this sort of test as it will allow things that here you would definitely not want allowed as numeric. For example IsNumeric would return True for
"4e4"
"3,5,6$"
"23,,,,,.3"
etc
 
Upvote 0
Good morning. I will try these this morning. Thank you so much!!!
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,072
Members
449,093
Latest member
ripvw

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