By: Md. Sourov Hossain Mithun

In reply to megan m.

Hello MEGAN M,
Hope, you are doing well. Here’s the modified code below that will spell only whole numbers. Also, it will extract the whole number before spelling, if you insert decimal numbers.

Option Explicit
Function spelling_number(ByVal given_number)
Dim us_dollars, temp
Dim count
ReDim Position(9) As String
Position(2) = " Thousand "
Position(3) = " Million "
Position(4) = " Billion "
Position(5) = " Trillion "
given_number = Trim(Str(given_number))

' Extract the integer part of the number
Dim integerPart As Double
integerPart = Int(given_number)

count = 1
Do While integerPart <> 0
temp = GetHundreds(integerPart Mod 1000) ' Process the last three digits
If temp <> "" Then us_dollars = temp & Position(count) & us_dollars
integerPart = Int(integerPart / 1000) ' Remove the last three digits
count = count + 1
Loop

' Check if the result is an empty string, and if so, set it to "Zero"
If us_dollars = "" Then
us_dollars = "Zero"
End If

spelling_number = us_dollars
End Function

Function GetHundreds(ByVal given_number)
Dim output As String
If Val(given_number) = 0 Then Exit Function
given_number = Right("000" & given_number, 3)
If Mid(given_number, 1, 1) <> "0" Then
output = GetDigit(Mid(given_number, 1, 1)) & " Hundred "
End If
If Mid(given_number, 2, 1) <> "0" Then
output = output & GetTens(Mid(given_number, 2))
Else
output = output & GetDigit(Mid(given_number, 3))
End If
GetHundreds = output
End Function
Function GetTens(tens_text)
Dim output As String
output = ""
If Val(Left(tens_text, 1)) = 1 Then
Select Case Val(tens_text)
Case 10: output = "Ten"
Case 11: output = "Eleven"
Case 12: output = "Twelve"
Case 13: output = "Thirteen"
Case 14: output = "Fourteen"
Case 15: output = "Fifteen"
Case 16: output = "Sixteen"
Case 17: output = "Seventeen"
Case 18: output = "Eighteen"
Case 19: output = "Nineteen"
Case Else
End Select
Else
Select Case Val(Left(tens_text, 1))
Case 2: output = "Twenty "
Case 3: output = "Thirty "
Case 4: output = "Forty "
Case 5: output = "Fifty "
Case 6: output = "Sixty "
Case 7: output = "Seventy "
Case 8: output = "Eighty "
Case 9: output = "Ninety "
Case Else
End Select
output = output & GetDigit _
(Right(tens_text, 1))
End If
GetTens = output
End Function
Function GetDigit(number)
Select Case Val(number)
Case 1: GetDigit = "One"
Case 2: GetDigit = "Two"
Case 3: GetDigit = "Three"
Case 4: GetDigit = "Four"
Case 5: GetDigit = "Five"
Case 6: GetDigit = "Six"
Case 7: GetDigit = "Seven"
Case 8: GetDigit = "Eight"
Case 9: GetDigit = "Nine"
Case Else: GetDigit = ""
End Select
End Function

Thanks and regards,
Md. Sourov Hossain Mithun
Team ExcelDemy.