Find Largest Number from any text value


This code will clean up any dirty value like this (#$%^skfslf2a5fs7a8j22hf) and return the max number.It will extract the largest value from the textbox.


'Pass the value from the textbox txtValue to the function
'GetMaxValue.The function will extract largest value
'from the textbox.
Private Function GetMaxValue(ByVal txtValue As String) As Double
Dim I As Double
Dim J As Double
Dim strMain As String
Dim varNum As String
Dim varArrayNum(100) As String
Dim maxNum As Double

strMain = Trim(txtValue)
'MAIN LOOP
For J = 1 To Len(strMain)
'LOOP FOR EXTRACTING TEXT
If Asc(Mid(strMain, J, 1)) >= 48 And Asc(Mid(strMain, J, 1)) <= 57 Then
varNum = varNum & Mid(strMain, J, 1)
ElseIf varNum <> "" And Not IsDBNull(varNum) Then
varNum = varNum & ";"
End If
Next J

'STORE THE NUMERIC VALUES IN ARRAY
varArrayNum = Split(varNum, ";", -1, CompareMethod.Text)

'FIND THE LARGEST THE NUMBER OF ALL NUMBERS EXTRACTED
For J = LBound(varArrayNum) To UBound(varArrayNum)
If Not IsDBNull(varArrayNum(J)) And Not varArrayNum(J) = "" Then
maxNum = CInt(varArrayNum(J))
For I = LBound(varArrayNum) To UBound(varArrayNum)
If Not IsDBNull(varArrayNum(I)) And Not varArrayNum(I) = "" Then
If CInt(varArrayNum(I)) > maxNum Then
maxNum = varArrayNum(I)
End If
Next I
End If
Next J
return maxNum
End Function


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: