[英]How to convert a numeric value to words Bangladeshi Currency in Excel using VBA
This may be more than you bargained for, but that may be better than if it were less.这可能比您讨价还价的要多,但这可能比少一些要好。 Try it.
试试吧。 But first, please understand the setup.
但首先,请了解设置。 The idea is that you have a cell - in a worksheet, of course - in which you enter an amount.
这个想法是你有一个单元格——当然是在工作表中——你可以在其中输入一个数量。 Then you have another cell - presumed to be on the same worksheet, but not necessarily so - in which to display the amount in words.
然后你有另一个单元格 - 假定在同一个工作表上,但不一定如此 - 在其中以文字显示数量。 Paste the calling procedure which follows here in the code sheet of the worksheet on which you have the cell to contain the amount.
将此处后面的调用过程粘贴到您有包含金额的单元格的工作表的代码表中。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Const AmountCell As String = "B5" ' read the amount from here
Const TargetCell As String = "D5" ' write the words here
Const Indian As Boolean = True
If Target.Address = Range(AmountCell).Address Then
Call WriteAmountInWords(Target.Value, Range(TargetCell), Indian)
End If
End Sub
This code has a lot parameters you can set.这段代码有很多参数可以设置。 They start in the above procedure.
他们从上述程序开始。 The amount will be written in cell B5.
该金额将写入单元格 B5。 You can specify any other cell.
您可以指定任何其他单元格。 You can add code to specify several cells.
您可以添加代码以指定多个单元格。 If you want the same action on another sheet you need to paste a copy of the code in that other sheet's code sheet as well.
如果您想在另一张纸上执行相同的操作,您还需要将代码副本粘贴到另一张纸的代码表中。
The above code specifies the words to be written to cell D5.上面的代码指定要写入单元格 D5 的单词。 You can specify any other cell implicitly or relative to the AmountCell.
您可以隐式或相对于 AmountCell 指定任何其他单元格。 That is another task of programming not covered here.
这是此处未涉及的另一项编程任务。
Finally, you can specify Indian
as either True or False.最后,您可以将
Indian
指定为 True 或 False。 If you specify it as True you get lakhs and crores.如果您将其指定为 True,您将获得数十亿卢比。 False will produce millions and billions.
错误将产生数百万和数十亿。 If such is your need you can set this property programmatically, too.
如果这是您的需要,您也可以通过编程方式设置此属性。 Note, however, that the present structure doesn't lend itself to dynamic changes.
但是请注意,目前的结构不适合动态变化。 You would have to replace the constants with variables.
您将不得不用变量替换常量。
The above code call the procedure WriteAmountInWords
which has a lot of supporting code it needs.上面的代码调用了过程
WriteAmountInWords
,它有很多它需要的支持代码。 All of that must be on a new, normal (not class and not form) code module.所有这些都必须在一个新的、正常的(不是 class 也不是表单)代码模块上。 Its name isn't important, but you might call it
SpellNum
.它的名字并不重要,但您可以称它为
SpellNum
。 Paste all of the following code in that module.将以下所有代码粘贴到该模块中。
Option Explicit
Option Base 0
Enum Ncr ' Index to Array Curr()
NcrCurr
NcrOnly ' word used when there are no cents
NcrAnd ' word used between dollars and cents
NcrFraction
End Enum
Enum Nct ' CaseType
NctLower ' = all lower case
NctFirst ' = Only first character in upper case
NctProper ' = Each word's first character capitalised (Default)
NctUpper ' = all caps
End Enum
Enum Ngp ' Number groups: Powers of 1000
NgpN
NgpM ' = 1000's
NgpMM ' = millions
NgpBn ' = billions
NgpDec ' decimals
End Enum
Const SpellCurr As String = "dollar,only,and,cent"
Const Ones As String = "zero one two three four five six seven eight nine"
Const Teens As String = "teen eleven twelve thir four fif six seven eigh nine"
Const Tens As String = "null ten twenty thirty fourty fifty sixty seventy eighty ninety"
Const Powers_En As String = "hundred thousand million billion"
Const Powers_In As String = "hundred thousand lakh crore"
Dim Powers As String
Public Sub WriteAmountInWords(ByVal Amt As Variant, _
ByRef TargetCell As Range, _
ByVal Indian As Boolean)
Const WithCurr As Boolean = False
Const NoDecs As Boolean = False
Const SpellDecs As Boolean = False
Const CaseType As Long = NctProper
TargetCell.Value = SpellAmount(Amt, Indian, WithCurr, NoDecs, SpellDecs, CaseType)
End Sub
Private Function SpellAmount(ByVal Amt As Variant, _
ByVal Indian As Boolean, _
ByVal WithCurr As Boolean, _
ByVal NoDecs As Boolean, _
ByVal SpellDecs As Boolean, _
ByVal CaseType As Long) As String
' return the amount Amt in words
' include the currency, if WithCurr = True
' True to suppress zero fractions in integers,
' also ignore fractions existing in Amt
' write out fractions, if SpellDecs = True
' specify any Nct value for CaseType (Proper by default)
Dim Num As Double ' = Amt
Dim Spa As String ' result
Dim S As String ' partial result
Dim Sp() As String ' groups of numbers
Dim G As Ngp
Powers = IIf(Indian, Powers_In, Powers_En)
Num = SetGroups(Amt, Sp, Indian)
For G = NgpBn To NgpN Step -1
If Val(Sp(G)) > 0 Then
S = Spell999(Sp(G))
If G > NgpN Then
S = WithBreak(S, True) & Split(Powers)(G)
End If
Spa = WithBreak(Spa, True) & S
End If
Next G
If Len(Spa) = 0 Then Spa = Split(Ones)(0)
If NoDecs Then
If WithCurr Then Call AddCurrency(Spa, Int(Num))
Else
Call AddDecimals(Spa, Sp(NgpDec), SpellDecs, WithCurr, Num)
End If
SpellAmount = WriteProper(Spa, CaseType)
End Function
Private Function Spell999(G3 As String) As String
' return the amount in words of a G3 of 3 numbers
Dim Sp As String ' result
Dim S As String ' partial result
Dim n(1 To 3) As Integer ' value of each character
Dim IsTeen As Boolean
Dim i As Long
For i = 1 To 3
n(i) = Val(Mid(Right("000" & G3, 3), i, 1))
Next i
If n(1) > 0 Then Sp = WithBreak((Split(Ones)(n(1)))) & _
Split(Powers)(NgpN)
If n(2) = 1 And n(3) > 0 Then
IsTeen = True
ElseIf n(2) Then
Sp = WithBreak(Sp) & Split(Tens)(n(2))
End If
If n(3) Then
If IsTeen Then
S = Split(Teens)(n(3))
If n(3) > 2 Then
S = WithBreak(S) & Split(Teens)(0)
End If
Else
S = Split(Ones)(n(3))
End If
Sp = WithBreak(Sp) & S
End If
Spell999 = Sp
End Function
Private Sub AddDecimals(ByRef Spa As String, _
ByVal Decs As String, _
ByVal SpellDecs As Boolean, _
ByVal WithCurr As Boolean, _
ByVal Num As Double)
Dim S As String
If WithCurr And SpellDecs Then Call AddCurrency(S, Int(Num))
S = WithBreak(S, True) & Split(SpellCurr, ",") _
(NcrOnly - CBool(Val(Decs)))
If SpellDecs Then
If Val(Decs) Then
S = WithBreak(S, True) & Spell999(Decs)
If WithCurr Then
Call AddCurrency(S, Val(Decs), True)
Else
S = WithBreak(S, True) & Split(Powers)(0) & "th"
End If
End If
Else
S = WithBreak(S, True) & Decs & "/100"
If WithCurr Then Call AddCurrency(S, Num)
End If
Spa = WithBreak(Spa, True) & S
End Sub
Private Function SetGroups(ByVal Amt As Variant, _
ByRef Sp() As String, _
ByVal Indian As Boolean) As Double
' Sp() is a return array
Dim Grps() As Variant
Dim A As String
Dim n As Integer
Dim i As Integer
If Indian Then
Grps = Array(5, 2, 2, 3) ' from left to right
Else
Grps = Array(3, 3, 3, 3)
End If
ReDim Sp(NgpDec)
A = Format(Unformat(Amt), String(12, "0") & ".00")
For i = NgpN To (NgpDec - 1)
Sp(NgpDec - i - 1) = Mid(A, n + 1, Grps(i))
n = n + Grps(i)
Next i
Sp(NgpDec) = Right(A, 2)
SetGroups = Val(A)
End Function
Private Function Unformat(ByVal Amt As Variant) As String
Dim Uf As String
Dim S As String
Dim i As Integer
For i = 1 To Len(Amt)
S = Mid(Amt, i, 1)
If IsNumeric(S) Or S = "." Then
Uf = Uf & S
End If
Next i
Unformat = Uf
End Function
Private Function WithBreak(ByVal S As String, _
Optional ByVal AddSpace As Boolean) _
As String
' append a conditional line break or space to S
Dim BreakChar As Integer
BreakChar = IIf(AddSpace, 32, 31)
WithBreak = S
If Len(S) > 1 Then
If Asc(Right(S, 1)) <> BreakChar Then
WithBreak = S + Chr(BreakChar)
End If
End If
End Function
Private Function WriteProper(ByVal S As String, _
ByVal CaseType As Nct) As String
Dim Wp As String
Dim Sp() As String
Dim n As Long
If Len(S) Then
Wp = LCase(S)
Select Case CaseType
Case NctFirst
Wp = UCase(Left(S, 1)) & Mid(S, 2)
Case NctProper
Sp = Split(Wp)
For n = LBound(Sp) To UBound(Sp)
Sp(n) = UCase(Left(Sp(n), 1)) & Mid(Sp(n), 2)
Next n
Wp = Join(Sp)
Case NctUpper
Wp = UCase(S)
End Select
End If
WriteProper = Wp
End Function
Private Sub AddCurrency(ByRef Spa As String, _
ByVal Num As Double, _
Optional IsFraction As Boolean)
Dim S As String
Dim i As Ncr
i = IIf(IsFraction, NcrFraction, NcrCurr)
S = Split(SpellCurr, ",")(i) & IIf(Num = 1, "", "s")
Spa = WithBreak(Spa, True) & S
End Sub
Look for this line of code Const SpellCurr As String = "dollar,only,and,cent"
.寻找这行代码
Const SpellCurr As String = "dollar,only,and,cent"
。 Change the dollars to the name of your currency.将美元更改为您的货币名称。 Same for the "cents".
“分”也一样。 However, by default the words will be written without naming the currency.
但是,默认情况下,将在不命名货币的情况下写入文字。 You have to enable that by changing
Const WithCurr As Boolean = False
to True
.您必须通过将
Const WithCurr As Boolean = False
更改为True
来启用它。
This setting excludes decimals from the written amount.此设置不包括写入数量中的小数。
Const NoDecs As Boolean = False
. Const NoDecs As Boolean = False
。 You can change it to True
.您可以将其更改为
True
。 Once it is True
you can specify how to write the decimals, in words or numbers.一旦它为
True
,您就可以指定如何用文字或数字书写小数。 Const SpellDecs As Boolean = False
The default is False, meaning written as numbers, like 00/100. Const SpellDecs As Boolean = False
默认为 False,表示写成数字,如 00/100。
The last constant in the WriteAmountInWords
procedure determines capitalisation of the spelled out amount. WriteAmountInWords
过程中的最后一个常量决定拼写金额的大小写。 Const CaseType As Long = NctProper
. Const CaseType As Long = NctProper
。 To set this constant, use one of the enumerations at the top of the code (here repeated).要设置此常量,请使用代码顶部的枚举之一(此处重复)。
Enum Nct ' CaseType
NctLower ' = all lower case
NctFirst ' = Only first character in upper case
NctProper ' = Each word's first character capitalised (Default)
NctUpper ' = all caps
End Enum
Note that the capitalisation of the enuration names will adapt itself to your preference.请注意,枚举名称的大写会根据您的喜好自行调整。 Once you capitalise a name differently, VBA will remember and follow your guidance.
一旦您使用不同的大写字母,VBA 将记住并遵循您的指导。 Type responsibly.
负责任地打字。
I have updated the module for bangla.我已经更新了孟加拉语模块。 I have done it for my personal need.
我这样做是为了我个人的需要。 Now you can use too.
现在你也可以使用了。 Below is the download link.
下面是下载链接。 Right click and select save as.
右键单击并 select 另存为。
https://github.com/masudpce/number_to_bangla_word/raw/main/number_to_bangla%20_word_any_font.bas https://github.com/masudpce/number_to_bangla_word/raw/main/number_to_bangla%20_word_any_font.bas
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.