Need help to solve 'number to word' VBA code

sajids12bd

New Member
Joined
Mar 7, 2021
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
Can anyone help me on this VBA code. I just want to add 'and' in the last integer number. such as; 12520 - twelve thousand five hundred and twenty, or 12500 - twelve thousand and five hundred.

VBA Code:
Function BdTaka(ByVal N As Currency) As String

   Const hundred = 100@
   Const thousand = 1000@
   Const lac = 100000@
   Const crore = 10000000@
 
   If (N = 0@) Then BangladeshTaka = "Zero": Exit Function
 
   Dim Buf As String: If (N < 0@) Then Buf = "Negative " Else Buf = ""
   Dim Frac As Currency: Frac = Abs(N - Fix(N))
   If (N < 0@ Or Frac <> 0@) Then N = Abs(Fix(N))
   Dim AtLeastOne As Integer: AtLeastOne = N >= 1
 
   If (N >= crore) Then
      Buf = Buf & BangladeshTakaDigitGroup(Int(N / crore)) & " crore"
      N = N - Int(N / crore) * crore
      If (N >= 1@) Then Buf = Buf & " "
   End If
 
   If (N >= lac) Then
      Buf = Buf & BangladeshTakaDigitGroup(Int(N / lac)) & " lac"
      N = N - Int(N / lac) * lac
      If (N >= 1@) Then Buf = Buf & " "
   End If
 
   If (N >= thousand) Then
      Buf = Buf & BangladeshTakaDigitGroup(N \ thousand) & " thousand"
      N = N Mod thousand
      If (N >= 1@) Then Buf = Buf & " "
   End If
  
   If (N >= hundred) Then
      Buf = Buf & BangladeshTakaDigitGroup(N \ hundred) & " hundred"
      N = N Mod hundred
      If (N >= 1@) Then Buf = Buf & " and "
   End If
 
   If (N >= 0@) Then
      Buf = "" & Buf & BangladeshTakaDigitGroup(N) & ""
   End If
  
   If (Frac = 0@) Then
      Buf = Buf & ""
   Else
      If AtLeastOne Then Buf = Buf & " "
      Buf = Buf & " and " & BangladeshTakaDigitGroup(Frac * 100) & " Paisa"
   End If
 
   BdTaka = Buf & " only"
End Function
 
Private Function BangladeshTakaDigitGroup(ByVal N As Integer) As String
   Const One = "one"
   Const Two = "two"
   Const Three = "three"
   Const Four = "four"
   Const Five = "five"
   Const Six = "six"
   Const Seven = "seven"
   Const Eight = "eight"
   Const Nine = "nine"
   Dim Buf As String: Buf = ""
   Dim Flag As Integer: Flag = False
 
   Select Case (N \ 100)
      Case 0: Buf = "": Flag = False
      Case 1: Buf = One & hundred: Flag = True
      Case 2: Buf = Two & hundred: Flag = True
      Case 3: Buf = Three & hundred: Flag = True
      Case 4: Buf = Four & hundred: Flag = True
      Case 5: Buf = Five & hundred: Flag = True
      Case 6: Buf = Six & hundred: Flag = True
      Case 7: Buf = Seven & hundred: Flag = True
      Case 8: Buf = Eight & hundred: Flag = True
      Case 9: Buf = Nine & hundred: Flag = True
   End Select
 
   If (Flag <> False) Then N = N Mod 100
   If (N > 0) Then
      If (Flag <> False) Then Buf = Buf & " "
   Else
      BangladeshTakaDigitGroup = Buf
      Exit Function
   End If
 
   Select Case (N \ 10)
      Case 0, 1: Flag = False
      Case 2: Buf = Buf & "twenty": Flag = True
      Case 3: Buf = Buf & "thirty": Flag = True
      Case 4: Buf = Buf & "forty": Flag = True
      Case 5: Buf = Buf & "fifty": Flag = True
      Case 6: Buf = Buf & "sixty": Flag = True
      Case 7: Buf = Buf & "seventy": Flag = True
      Case 8: Buf = Buf & "eighty": Flag = True
      Case 9: Buf = Buf & "ninety": Flag = True
   End Select
 
   If (Flag <> False) Then N = N Mod 10
   If (N > 0) Then
      If (Flag <> False) Then Buf = Buf & " "
   Else
      BangladeshTakaDigitGroup = Buf
      Exit Function
   End If
 
   Select Case (N)
      Case 0:
      Case 1: Buf = Buf & One
      Case 2: Buf = Buf & Two
      Case 3: Buf = Buf & Three
      Case 4: Buf = Buf & Four
      Case 5: Buf = Buf & Five
      Case 6: Buf = Buf & Six
      Case 7: Buf = Buf & Seven
      Case 8: Buf = Buf & Eight
      Case 9: Buf = Buf & Nine
      Case 10: Buf = Buf & "ten"
      Case 11: Buf = Buf & "eleven"
      Case 12: Buf = Buf & "twelve"
      Case 13: Buf = Buf & "thirteen"
      Case 14: Buf = Buf & "fourteen"
      Case 15: Buf = Buf & "fifteen"
      Case 16: Buf = Buf & "sixteen"
      Case 17: Buf = Buf & "seventeen"
      Case 18: Buf = Buf & "eighteen"
      Case 19: Buf = Buf & "nineteen"
   End Select
 
   BangladeshTakaDigitGroup = Buf
 
End Function
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Need help to solve 'number to word' VBA code
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
You should find the alternate code I have posted in my mini-blog article here to be useful...

Yet Another Number-To-Words Function (Sorry, US Style Only)

One of the output options (use of the optional argument "And") will give you the output you asked for. In addition, there are three other output modes that you might find useful in the future. And, while probably not a concern for you, the function will handle extremely large numbers, namely, a fraction less than a quintillion... that is a number with 18 digits in front of the decimal point!
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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