Kalim Shaikh
New Member
- Joined
- Jun 13, 2023
- Messages
- 24
- Office Version
- 2021
- Platform
- Windows
- Mobile
- 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)
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: