💸 Convert Numbers to Text in Excel (Tamil) | Spell Number Formula 🔥
Code:
Option Explicit
' Public function you use in Excel: =SpellRupees(A1)
Public Function SpellRupees(ByVal n As Variant) As String
Dim s As String, intPart As String, decPart As String
Dim rupeesWords As String, resultText As String
If IsNull(n) Or Trim(CStr(n)) = "" Then
SpellRupees = ""
Exit Function
End If
s = Replace(Trim(CStr(n)), ",", "") ' remove commas if any
' Split integer/decimal parts (paise)
If InStr(1, s, ".", vbTextCompare) > 0 Then
intPart = Split(s, ".")(0)
decPart = Left(Split(s, ".")(1) & "00", 2) ' 2 digits paise, no rounding up beyond 99
Else
intPart = s
decPart = ""
End If
If Val(intPart) = 0 Then
rupeesWords = "Zero"
Else
rupeesWords = IndianWords(intPart)
End If
resultText = "Rupees " & rupeesWords
If decPart <> "" And Val(decPart) > 0 Then
resultText = resultText & " and " & TwoDigitWords(CInt(decPart)) & " Paise"
End If
SpellRupees = resultText & " Only"
End Function
' Convert whole-number string (no commas/decimals) to Indian words
Private Function IndianWords(ByVal digits As String) As String
Dim res As String, chunk As String, idx As Long
Dim lenD As Long
digits = Trim(digits)
If digits = "" Then IndianWords = "Zero": Exit Function
If Left(digits, 1) = "-" Then
IndianWords = "Minus " & IndianWords(Mid(digits, 2))
Exit Function
End If
lenD = Len(digits)
If lenD <= 3 Then
IndianWords = ThreeDigitWords(CInt(digits))
Exit Function
End If
' Last 3 digits (hundreds)
res = ThreeDigitWords(CInt(Right(digits, 3)))
digits = Left(digits, lenD - 3)
' Then take 2 digits at a time for Thousand, Lakh, Crore, Arab, Kharab...
idx = 0
Do While Len(digits) > 0
idx = idx + 1
If Len(digits) > 2 Then
chunk = Right(digits, 2)
digits = Left(digits, Len(digits) - 2)
Else
chunk = digits
digits = ""
End If
If Val(chunk) > 0 Then
If res <> "" Then
res = TwoDigitWords(CInt(chunk)) & " " & SectionName(idx) & " " & res
Else
res = TwoDigitWords(CInt(chunk)) & " " & SectionName(idx)
End If
End If
Loop
IndianWords = Trim(res)
End Function
' Map section index → Indian scale names
Private Function SectionName(ByVal idx As Long) As String
Select Case idx
Case 1: SectionName = "Thousand"
Case 2: SectionName = "Lakh"
Case 3: SectionName = "Crore"
Case 4: SectionName = "Arab" ' optional higher units
Case 5: SectionName = "Kharab" ' optional higher units
Case Else: SectionName = ""
End Select
End Function
' 0–999
Private Function ThreeDigitWords(ByVal n As Integer) As String
Dim res As String
If n = 0 Then Exit Function
If n >= 100 Then
res = DigitWord(n \ 100) & " Hundred"
If (n Mod 100) > 0 Then res = res & " " & TwoDigitWords(n Mod 100)
Else
res = TwoDigitWords(n)
End If
ThreeDigitWords = res
End Function
' 0–99
Private Function TwoDigitWords(ByVal n As Integer) As String
Dim ones As Variant, tens As Variant
ones = Array("Zero", "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")
If n < 20 Then
TwoDigitWords = ones(n)
Else
TwoDigitWords = tens(n \ 10)
If (n Mod 10) > 0 Then TwoDigitWords = TwoDigitWords & " " & ones(n Mod 10)
End If
End Function
Private Function DigitWord(ByVal n As Integer) As String
Dim ones As Variant
ones = Array("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine")
If n >= 0 And n <= 9 Then DigitWord = ones(n) Else DigitWord = ""
End Function
0 Comments