Excel VBA Number Format Validation

I'm using Excel VBA to validate data submissions in an Excel sheet. One of the tasks is to ensure that the entry is a valid number. The isnumeric() function works just fine for everything except one kind of error.

Sometimes the entry is "23,14" instead of "23.14", both of which isnumeric() returns as True. I've tried intc() and round(), but both of those translate it to 2314. I'd like to be able to flag "23,14" as invalid while leaving "23,141" as valid, of course.
LVL 4
PaulRKruegerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulRKruegerAuthor Commented:
This doesn't seem to work for me. It says that "23,23" is a valid number.
0
Ken ButtersCommented:
Thought I checked it out better than that.

Ok... I just wrote this one... see if this works better.

Modified solution to allow no commas or correct commas

Function IsNumber(ByVal Value As String) As Boolean
    Dim partsOfNumber() As String
    Dim checkCommas As String
    
    
    If Not IsNumeric(Value) Then
        IsNumber = False
        Exit Function
    End If
    
    partsOfNumber = Split(Value, ".")
    
    If UBound(partsOfNumber) > 2 Then
        IsNumber = False
        Exit Function
    End If
    
    If InStr(partsOfNumber(0), ",") = 0 Then
        IsNumber = True
        Exit Function
    End If
    
    checkCommas = Format(partsOfNumber(0), "#,###")
    If checkCommas <> partsOfNumber(0) Then
        IsNumber = False
        Exit Function
    End If
    
    IsNumber = True
    

End Function

Open in new window

0
[ fanpages ]IT Services ConsultantCommented:
Hi,

This statement will return False:

Format(Replace("23,14",",",""),"#,###,##0")="23,14"

This statement will return True:

Format(Replace("23,141",",",""),"#,###,##0")="23,141"

Depending on the potential maximum number of digits allowed, the "#,###,##0" format may need to be increased (to say "#,###,###,##0"), but hopefully you can work with the statement (replacing "23,14" or "23,141" with the entry to be validated).

For example:

Public Function blnIsValid(ByVal strValue As String) As Boolean

  If InStr(strValue, ",") > 0 Then
     blnIsValid = Format(Replace(strValue, ",", ""), "#,###,##0") = strValue
  Else
     blnIsValid = IsNumeric(strValue)
  End If ' If InStr(strValue, ",") > 0 Then
  
End Function

Open in new window


Usage:

?blnIsValid("23,141") <- True
?blnIsValid("23,14") <- False
?blnIsValid("23,141,12") <- False
?blnIsValid("23,141,121") <- True

BFN,

fp.
Q-28234547.xls
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
[ fanpages ]IT Services ConsultantCommented:
Has this thread been abandoned?
0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.