Convert Number to Words

Kalim Shaikh

New Member
Joined
Jun 13, 2023
Messages
24
Office Version
  1. 2021
Platform
  1. Windows
  2. Mobile
  3. Web
Please help me on this I am using the below VBA code as a result I get the word "Rupees" at the beginning of the result and the word "Only" at the end.

Example
123
=Spell Number(One Hundred and Twenty Three )
Need Result
Rupees One Hundred and Twenty Three Only

(Using Below Code)

VBA Code:
Option Explicit

Function SpellNumber(ByVal MyNumber)

Dim uStr As String

Dim uFNum As Integer

Dim uStrPoint

Dim uStrNumber

Dim uPoint As String

Dim uNumber As String

Dim uP() As Variant

Dim uDP

Dim uCnt As Integer

Dim uResult, uT As String

Dim uLen As Integer

On Error Resume Next

uP = Array("", "Thousand ", "Million ", "Billion ", "Trillion ", " ", " ", " ", " ")

uNumber = Trim(Str(MyNumber))

uDP = InStr(uNumber, ".")

uPoint = ""

uStrNumber = ""

If uDP > 0 Then

uPoint = " point "

uStr = Mid(uNumber, uDP + 1)

uStrPoint = Left(uStr, Len(uNumber) - uDP)

For uFNum = 1 To Len(uStrPoint)

uStr = Mid(uStrPoint, uFNum, 1)

uPoint = uPoint & Digits(uStr) & " "

Next uFNum

uNumber = Trim(Left(uNumber, uDP - 1))

End If

uCnt = 0

uResult = ""

uT = ""

uLen = 0

uLen = Int(Len(Str(uNumber)) / 3)

If (Len(Str(uNumber)) Mod 3) = 0 Then uLen = uLen - 1

Do While uNumber <> ""

If uLen = uCnt Then

uT = HundredsDigits(Right(uNumber, 3), False)

Else

If uCnt = 0 Then

uT = HundredsDigits(Right(uNumber, 3), True)

Else

uT = HundredsDigits(Right(uNumber, 3), False)

End If

End If

If uT <> "" Then

uResult = uT & uP(uCnt) & uResult

End If

If Len(uNumber) > 3 Then

uNumber = Left(uNumber, Len(uNumber) - 3)

Else

uNumber = ""

End If

uCnt = uCnt + 1

Loop

uResult = uResult & uPoint

SpellNumber = uResult

End Function

Function HundredsDigits(uHDgt, uB As Boolean)

Dim uRStr As String

Dim uStrNum As String

Dim uStr As String

Dim uI As Integer

Dim uBB As Boolean

uStrNum = uHDgt

uRStr = ""

On Error Resume Next

uBB = True

If Val(uStrNum) = 0 Then Exit Function

uStrNum = Right("000" & uStrNum, 3)

uStr = Mid(uStrNum, 1, 1)

If uStr <> "0" Then

uRStr = Digits(Mid(uStrNum, 1, 1)) & "Hundred "

Else

If uB Then

uRStr = "and "

uBB = False

Else

uRStr = " "

uBB = False

End If

End If

If Mid(uStrNum, 2, 2) <> "00" Then

uRStr = uRStr & TenDigits(Mid(uStrNum, 2, 2), uBB)

End If

HundredsDigits = uRStr

End Function

Function TenDigits(uTDgt, uB As Boolean)

Dim uStr As String

Dim uI As Integer

Dim uArr_1() As Variant

Dim uArr_2() As Variant

Dim uT As Boolean

uArr_1 = Array("Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen ")

uArr_2 = Array("", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety ")

uStr = ""

uT = True

On Error Resume Next

If Val(Left(uTDgt, 1)) = 1 Then

uI = Val(Right(uTDgt, 1))

If uB Then uStr = "and "

uStr = uStr & uArr_1(uI)

Else

uI = Val(Left(uTDgt, 1))

If Val(Left(uTDgt, 1)) > 1 Then

If uB Then uStr = "and "

uStr = uStr & uArr_2(Val(Left(uTDgt, 1)))

uT = False

End If

If uStr = "" Then

If uB Then

uStr = "and "

End If

End If

If Right(uTDgt, 1) <> "0" Then

uStr = uStr & Digits(Right(uTDgt, 1))

End If

End If

TenDigits = uStr

End Function

Function Digits(uDgt)

Dim uStr As String

Dim uArr_1() As Variant

uArr_1 = Array("Zero ", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ")

uStr = ""

On Error Resume Next

uStr = uArr_1(Val(uDgt))

Digits = uStr

End Function
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try changing:
VBA Code:
SpellNumber = uResult
To:
VBA Code:
SpellNumber = "Rupees " & uResult & " Only"
 
Upvote 0
Solution
Brilliant Sir

Georgiboy

Thanks for the guidance. It is working very well. May Allah give you more progress in your knowledge. Aamen
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top