Checking string to only contain numerical characters

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
339
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
I need help in writing a statement that can extend this if statement to ensure that in addition to haveing no spaces that the string has only "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" Not sure where to turn to...

If (InStr(rsSNReport!StudentID, " ") <> 0) Or (Len(Trim(rsSNReport!StudentID)) <> 9) Then ...

Thanks in advance for your assistance.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I think that I was not clear. I need the string to contain only numbers (0 - 9), no spaces, no letters, no symbols. The string will need to be 9 digits long.

From what I see in your answer, it looks to check for a string being 1 digit.
 
Upvote 0
Ah. You fooled me with
Code:
If (InStr(rsSNReport!StudentID, " ") <> 0) Or (Len(Trim(rsSNReport!StudentID)) <> 9) Then ...

So, to do what you want you'd need to know what to do with a string that is "abc1234546". Should this function return 123456 or nothing?

Barring that I'd imagine a loop like

Code:
Function CheckNum(strInput As String) As Boolean
Dim temp As Boolean
Dim i As Long
If Len(strInput) = 9 Then
  For i = 1 To Len(strInput)
    Select Case Val(Mid(strInput, i, 1))
      Case 0 To 9
        temp = True
      Case Else
        temp = False
        Exit For
    End Select
  Next i
End If
CheckNum = temp
End Function

Give or take...

hth,

Rich
 
Upvote 0
If it has any "illegal characters" it should return true to execute my error correcting steps/prompts.
 
Upvote 0
I would switch the two Temp=True/Temp=False statements to accomplish that requirement.

hth,

Rich
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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