напиши макрос для эксель, который может записать число словами на английском
- User
напиши макрос для эксель, который может записать число словами на английском
- Assistant
напиши макрос для эксель, который может записать число словами на английском
Sub NumberToWords() Dim Num As Currency Dim Dollars As String, Cents As String Dim Temp As String Dim DollarsAnd As String Dim DecimalPlace, Count Dim Place(9) As String Place(2) = " Thousand " Place(3) = " Million " Place(4) = " Billion " Place(5) = " Trillion "
Num = ActiveCell.Value
If Abs(Num) >= 1000000000000# Then
Num = ""
ActiveCell.Value = "Number is too large to convert to words"
Exit Sub
End If
' Convert the number to dollars and cents
Num = Round(Num, 2)
Dollars = Int(Num)
Cents = Format((Num - Dollars) * 100, "00")
' Convert dollars to words
Temp = ""
DollarsAnd = ""
DecimalPlace = 0
Do Until Dollars = 0
Count = Len(Dollars)
Select Case Count
Case 1 To 3
Temp = GetDigit(Mid(Dollars, Count, 1)) & IIf(Temp = "", "", " ") & Temp
Case 4 To 6
Temp = GetTens(Mid(Dollars, Count - 1, 2)) & IIf(Temp = "", "", " ") & Temp
Temp = GetDigit(Mid(Dollars, Count, 1)) & IIf(Temp = "", "", " ") & Temp
Case 7 To 9
Temp = GetHundreds(Mid(Dollars, Count - 2, 3)) & IIf(Temp = "", "", " ") & Temp
Temp = GetTens(Mid(Dollars, Count - 1, 2)) & IIf(Temp = "", "", " ") & Temp
Temp = GetDigit(Mid(Dollars, Count, 1)) & IIf(Temp = "", "", " ") & Temp
Case Else
End Select
If Count > 3 Then
Dollars = Left(Dollars, Count - 3)
DollarsAnd = IIf(Temp = "", "", Temp & Place(DecimalPlace) & DollarsAnd)
Temp = ""
DecimalPlace = DecimalPlace + 1
Else
DollarsAnd = Temp & DollarsAnd
Temp = ""
Dollars = ""
End If
Loop
' Convert cents to words
If Cents <> "" Then
Cents = " and " & GetTens(Cents)
End If
' Combine dollars and cents
If DollarsAnd <> "" Then
ActiveCell.Value = DollarsAnd & "Dollars" & Cents & " Cents"
Else
ActiveCell.Value = "Zero Dollars" & Cents & " Cents"
End If
End Sub
Function GetDigit(Digit) Select Case Val(Digit) Case 1: GetDigit = "One" Case 2: GetDigit = "Two" Case 3: GetDigit = "Three" Case 4: GetDigit = "Four" Case 5: GetDigit = "Five" Case 6: GetDigit = "Six" Case 7: GetDigit = "Seven" Case 8: GetDigit = "Eight" Case 9: GetDigit = "Nine" Case Else: GetDigit = "" End Select End Function
Function GetTens(TensText) Dim Result As String Result = "" If Val(Left(TensText, 1)) = 1 Then Select Case Val(TensText) Case 10: Result = "Ten" Case 11: Result = "Eleven" Case 12: Result = "Twelve" Case 13: Result = "Thirteen" Case 14: Result = "Fourteen" Case 15: Result = "Fifteen" Case 16: Result = "Sixteen" Case 17: Result = "Seventeen" Case 18: Result = "Eighteen" Case 19: Result = "Nineteen" Case Else End Select Else Select Case Val(Left(TensText, 1)) Case 2: Result = "Twenty " Case 3: Result = "Thirty " Case 4: Result = "Fourty " Case 5: Result = "Fifty " Case 6: Result = "Sixty " Case 7: Result = "Seventy " Case 8: Result = "Eighty " Case 9: Result = "Ninety " Case Else End Select Result = Result & GetDigit(Right(TensText, 1)) End If GetTens = Result End Function
Function GetHundreds(HundredsText) Dim Result As String Result = "" If Val(HundredsText) = 0 Then Exit Function HundredsText = Right("000" & HundredsText, 3) ' Convert the hundreds place. If Left(HundredsText, 1) <> "0" Then Result = GetDigit(Left(HundredsText, 1)) & " Hundred " End If ' Convert the tens and ones place. Result = Result & GetTens(Right(HundredsText, 2)) GetHundreds = Result End Function
Not yet included in a published release.