Determine if a string is all numerals


I am extracting an string of length 8 from a filename. I want to convert the string to a date so I need to check if it is composed of all numeric digits before I try to convert it or I will cause an error.

So basically I need to determine if the string is composed of all numeric digits or if there are some text digits instead.


"12345678"                       this is good
"1234r567"                       any combination with any amount letters is bad

I'll probably have to loop through the components and check to see if they are numeric.

I will use this in existing code to decide whether to process the file or delete it.


Who is Participating?
dbbishopConnect With a Mentor Commented:
The problem with using IsNumeric is that 1234.43 is numeric and -342 is numeric, although neither are all numbers. Try the code below.
Declare Public Function NumCheck(Value as Variant) As Boolean
Dim I as Integer
NumCheck = 1
For I = 1 To Len(Value)
  If Mid(Value, I, 1) < '0' Or Mid(Value, I, 1) > '9' Then 
    NumCheck = 0
    Exit Function
Next I

Then execute by using:
 If NumCheck(myValue) = 0 Then ' not all numeric

Open in new window

Rey Obrero (Capricorn1)Commented:

you can use the isnumeric() function

if isnumeric(string) then

end if
Use the IsNumeric() Function, so something like this:

Dim strCheck as String
strCheck = "12345678"

If IsNumeric(strCheck) Then
    'Do what you want here, it is a valid number
    'Not all numbers, do what you want here
End If
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

While I agree with your point, I think that just ends up opening another can of worms.

He says this value will then be converted to a date, so should (s)he be checking for valid numbers that can also be converted to a valid date, and within what time frame of date allowed (only this week? last week? within a year or two), etc. etc.

I think it all boils down to how much this user needs out of this and what his/her full requirement is.

- Anthony
Rey Obrero (Capricorn1)Commented:
i agree with dbbishop, but for the OP's requirement the isNumeric() function will do since
the string will be coming from ....

<I am extracting an string of length 8 from a filename.>

and i doubt that you will find a "-" sign at the start of the string and a dot(.) in the middle of the string...
However, if I recall correctly, 123e34 is also numeric using the IsNumeric() function.
Rey Obrero (Capricorn1)Commented:
<123e34 is also numeric using the IsNumeric() function.>

correct, but not when you pass is as a String..

Patrick MatthewsCommented:
>>However, if I recall correctly, 123e34 is also numeric using the IsNumeric() function.Indeed, it will :)
Rey Obrero (Capricorn1)Commented:
i stand corrected...
Patrick MatthewsConnect With a Mentor Commented:
If your real concern is to validate that a string can convert to a date, then assuming your string is yyyymmdd...If IsDate(Left(MyStr, 4) & "-" & Mid(MyStr, 5, 2) & "-" & Right(MyStr, 2)) Then   'valid dateElse    'invalidEnd IfIf the string is, say, mmddyyyy....If IsDate(Right(MyStr, 4) & "-" & Left(MyStr, 2) & "-" & Mid(MyStr, 3, 2)) Then   'valid dateElse    'invalidEnd If
Patrick MatthewsCommented:
In that code I just posted, if the expression passed to IsDate cannot be converted to a Date value, then the function simply returns False.IsDate will work in queries, too.
Patrick MatthewsCommented:
And, of course, if the string is ddmmyyyy....If IsDate(Right(MyStr, 4) & "-" & Mid(MyStr, 3, 2) & "-" & Left(MyStr, 2)) Then  'valid dateElse   'invalidEnd If
Patrick MatthewsCommented:
And lastly, ignoring dates for a moment, if all you really want to do is ensure that a given string contains only digits...If Not MyString Like "*[!0-9]*" Then    'code for only digitsElse    'code for at least one non-digitEnd IfThat will work in queries too.
scurvylionAuthor Commented:
Thanks for all of your input guys. First of all, let me apologize for not being as clear as I could of been. I shouldn't have gotten lazy when writing the question up.

MatthewsPatrick hit the nail on the head with the solution for yyyymmdd but since I asked for a solution to whether the string was numeric or not, dbbishop also provided a valid solution. Therefore, I am going to split the points between you two guys.

I appreciate your timely advice.
scurvylionAuthor Commented:
Thanks again guys.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.