Attachment 174047
I'm writing a function NumberToWords which converts a number to text and ran into an issue. I'm trying to figure out programatically what is the threshold or trigger that makes VBA automatically convert a number into scientific notation format. I have included more code than Ii wanted to but can't see how to shorten it and get you enough information to see what i am doing. The below samples show why I'm asking the question and need to handle scientific notation conversions. I am trying to avoid passing numbers as "text". Am I missing an obvious fix?
?NumberToWords("324356842398732872.205") <-- string delimited processes as expected
Three Two Four Three Five Six Eight Four Two Three Nine Eight Seven Three Two Eight Seven Two Point Two Zero Five
?NumberToWords(324356842398732872.205) <-- Number passed as variant doesn't.
Three Point Two Four Three Five Six Eight Four Two Three Nine Eight Seven Three Three Zero Zero One Seven
![]()
Function NumberToWords(MyNumber As Variant) 'Convert numbers to words 'DOES NOT PRODUCE CHECK CURRENCY FORMAT! ' designed to convert numberic digit to its corresponding word ' Supports decimals :) 'Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend Dim number As Integer Dim Words As String Dim i As Integer Dim DecimalPoint As Double ' Find decimal point. DecimalPoint = InStr(1, MyNumber, Application.DecimalSeparator) If fSNI(MyNumber) > 15 Then MsgBox "The Number automatically been converted to Scientific Notation." & vbCrLf & _ "Excel Number format currently only supports 15 digits. " & vbCrLf & vbCrLf & _ "If you need additional digits use a delimited string parameter.", vbCritical GoTo WrapUp End If For i = 1 To Len(MyNumber) ' Decimal point? If i = DecimalPoint Then NumberToWords = NumberToWords & " " & "Point" GoTo Skipcase End If number = Val(Mid(MyNumber, i, 1)) Select Case number Case 0 Words = "Zero" Case 1 To 99 Words = GetTens(number) End Select NumberToWords = NumberToWords & " " & Words Skipcase: Next i NumberToWords = Trim(NumberToWords) WrapUp: End Function
![]()
Function SetNums(Optional StrSystem As String = "American") Numbers = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen") Tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety") Select Case StrSystem Case "American" LrgNbr(0) = "Zero" LrgNbr(1) = "Hundred" LrgNbr(2) = "Thousand" For x = 1 To 32 'The rest of the Code will currently work up to LrgNbr 5-34?? LrgNbr(x + 2) = "" & Choose(x, "M", "B", "Tr", "Quadr", "Quint", "Sext", "Sept", "Oct", _ "Non", "Dec", "Undec", "Duodec", "Tredec", "Quattuordec", _ "Quindec", "Sexdec", "Septendec", "Octodec", "Novemdec", _ "Vigint", "Unvigint", "Duovigint", "Trevigint", "Quattuorvigint", _ "Quinvigint", "Sexvigint", "Septenvigint", "Octovigint", "Novemvigint", _ "Trigint", "Untrigint", "Duotrigint") & "illion" #If DEBUG_ Then Debug.Print "LrgNbr(" & x + 2 & ")" & LrgNbr(x + 2) #End If Next Case "Greek" ' I do NOT Speek Greek Need to verify terms. LrgNbr(0) = "Zero" LrgNbr(1) = "Hundred" LrgNbr(2) = "Thousand" For x = 1 To 31 LrgNbr(x + 2) = "" & Choose(x, "G", "Tetr", "Pent", "Hex", "Hept", "Okt", "Enn", "Dek", "Hendek", _ "Dodek", "Trisdek", "Tetradek", "Pentadek", "Hexadek", "Heptadek", _ "Oktadek", "Enneadek", "Icos", "Icosihen", "Icosid", "Icositr", _ "Icositetr", "Icosipent", "Icosihex", "Icosihept", "Icosiokt", _ "Icosienn", "Triacont", "Triacontahen", "Triacontad", _ "Triacontatr") & "illion" #If DEBUG_ Then Debug.Print "LrgNbr(" & x + 2 & ")" & LrgNbr(x + 2) #End If Next Case "European" ' Need to verify terms. LrgNbr(0) = "Zero" LrgNbr(1) = "Hundred" LrgNbr(2) = "Thousand" For x = 1 To 31 LrgNbr(x + 2) = "" & Choose(x, "M", "B", "B", "Tr", "Tr", "Quadr", "Quadr", "Quint", "Quint", _ "Sext", "Sext", "Sept", "Sept", "Oct", "Oct", "Non", "Non", "Dec", _ "Dec", "Undec", "Undec", "Duodec", "Duodec", "Tredec", "Tredec", _ "Quattuordec", "Quattuordec", "Quindec", "Quindec", "Sexdec", _ "Sexdec") & IIf(x Mod 2, "illiard ", "illion") #If DEBUG_ Then Debug.Print "LrgNbr(" & x + 2 & ")" & LrgNbr(x + 2) #End If Next Case Else MsgBox ("Unsupported") GoTo WrapUp: End Select WrapUp: End Function
![]()
Function GetTens(TensNum As Variant) As String ' Converts a number from 0 to 99 into text. If TypeName(Numbers) <> "Variant()" Then SetNums Dim MyNo As String Select Case Abs(Val(TensNum)) Case 0 GetTens = LrgNbr(0) Case 1 To 19 GetTens = Numbers(TensNum) Case 20 To 99 MyNo = Format(TensNum, "00") GetTens = Tens(Val(Left(TensNum, 1))) & " " & Numbers(Val(Right(TensNum, 1))) Case Else MyNo = Format(TensNum, "0") For x = 1 To Len(TensNum) + 1 GetTens = GetTens & " " & Numbers(Trim(Val(Mid(MyNo, x, 1)))) Next x End Select End Function
![]()
Function fSNI(ByVal MyNumber) As Integer 'find Next Significant Digit in Scientific Notation Numbers fSNI = Val(Right(Format(MyNumber, "0.00E+00"), Len(Format(MyNumber, "0.00E+00")) - InStr((Format(MyNumber, "0.00E+00")), "E"))) + 1 End Function
Bookmarks