|
|
CODE '============================================================================== ' NAME: IsFraction ' PURPOSE: Checks if a string value can be evaluated as a mathematical fraction ' RETURNS: True on any mathematically valid fraction representation. ' To check if the number is Mixed (whole number and fraction), use the ' IsMixedNum() function ' DEPENDANCIES: Office 2000 or later (or custom Split() function) ' ' 1/2 = True ' -1/2 = True ' 15/5 = True ' 1.5/3 = False ' 1/0 = False ' 1 1/2 = False ' 1-1/2 = False ' ' R01 2010/12/10 Initial Release ' '============================================================================== 'ErrHandler V3.01 Public Function IsFraction(sInput As String) As Boolean On Error GoTo Error_Proc Dim Ret As Boolean '========================= Dim v As Variant '========================= 'make sure there's a slash If InStr(1, sInput, "/") = 0 Then GoTo Exit_Proc End If 'split the string v = Split(sInput, "/") 'make sure theres only one slash If UBound(v) > 1 Then GoTo Exit_Proc End If 'make sure both elements are numeric If Not IsNumeric(v(0)) Then GoTo Exit_Proc If Not IsNumeric(v(1)) Then GoTo Exit_Proc 'check the first number If Int(CDec(v(0))) <> CDec(v(0)) Then GoTo Exit_Proc End If 'check the second number If Int(CDec(v(1))) <> CDec(v(1)) Then GoTo Exit_Proc End If 'make sure the second element isn't 0 If CDbl(v(1)) = 0 Then GoTo Exit_Proc End If 'all set Ret = True '========================= Exit_Proc: IsFraction = Ret Exit Function Error_Proc: Select Case Err.Number Case Else MsgBox "Error: " & Trim(Str(Err.Number)) & vbCrLf & _ "Desc: " & Err.Description & vbCrLf & vbCrLf & _ "Module: modIsMixedNum, Procedure: IsFraction" _ , vbCritical, "Error!" End Select Resume Exit_Proc Resume End Function
|
| This page was last modified 08:45, 6 April 2011. This page has been accessed 1,117 times. Disclaimers |