Showing posts with label Code đọc chữ thành số trong excel vba. Show all posts
Showing posts with label Code đọc chữ thành số trong excel vba. Show all posts

Monday, March 25, 2019

9. Code đọc số thành chữ trong excel vba


'1. Vietnamese
Public Function docso(chuyenso) As String
s09 = Array("", " M" & ChrW(7897) & "t", " hai", " ba", " b" & ChrW(7889) & "n", " n" & ChrW(259) & "m", " s" & ChrW(225) & "u", " b" & ChrW(7843) & "y", " t" & ChrW(225) & "m", " ch" & ChrW(237) & "n")
lop3 = Array("", " tri" & ChrW(7879) & "u", " ngh" & ChrW(236) & "n", " t" & ChrW(7927))
'Stop
If Trim(chuyenso) = "" Then
  DocSoUni = ""
ElseIf IsNumeric(chuyenso) = True Then
  If chuyenso < 0 Then dau = ChrW(226) & "m " Else dau = ""
  chuyenso = Application.WorksheetFunction.Round(Abs(chuyenso), 0)
  chuyenso = " " & chuyenso
  chuyenso = Replace(chuyenso, ",", "", 1)
  vt = InStr(1, chuyenso, "E")
  If vt > 0 Then
    sonhan = Val(Mid(chuyenso, vt + 1))
    chuyenso = Trim(Mid(chuyenso, 2, vt - 2))
    chuyenso = chuyenso & String(sonhan - Len(chuyenso) + 1, "0")
  End If
  chuyenso = Trim(chuyenso)
  sochuso = Len(chuyenso) Mod 9
  If sochuso > 0 Then chuyenso = String(9 - (sochuso Mod 12), "0") & chuyenso
  docso = ""
  i = 1
  lop = 1
  Do
    n1 = Mid(chuyenso, i, 1)
    n2 = Mid(chuyenso, i + 1, 1)
    n3 = Mid(chuyenso, i + 2, 1)
    baso = Mid(chuyenso, i, 3)
    i = i + 3
    If n1 & n2 & n3 = "000" Then
      If docso <> "" And lop = 3 And Len(chuyenso) - i > 2 Then s123 = " t" & ChrW(7927) Else s123 = ""
    Else
      If n1 = 0 Then
        If docso = "" Then s1 = "" Else s1 = " kh" & ChrW(244) & "ng tr" & ChrW(259) & "m"
      Else
        s1 = s09(n1) & " tr" & ChrW(259) & "m"
      End If
      If n2 = 0 Then
        If s1 = "" Or n3 = 0 Then
          s2 = ""
        Else
          s2 = " linh"
        End If
      Else
        If n2 = 1 Then s2 = " M" & ChrW(432) & ChrW(7901) & "i" Else s2 = s09(n2) & " m" & ChrW(432) & ChrW(417) & "i"
      End If
      If n3 = 1 Then
        If n2 = 1 Or n2 = 0 Then s3 = " M" & ChrW(7897) & "t" Else s3 = " m" & ChrW(7889) & "t"
      ElseIf n3 = 5 And n2 <> 0 Then
        s3 = " l" & ChrW(259) & "m"
      Else
        s3 = s09(n3)
      End If
      If i > Len(chuyenso) Then
        s123 = s1 & s2 & s3
      Else
        s123 = s1 & s2 & s3 & lop3(lop)
      End If
    End If
    lop = lop + 1
    If lop > 3 Then lop = 1
    docso = docso & s123
    If i > Len(chuyenso) Then Exit Do
  Loop
  If docso = "" Then docso = "kh" & ChrW(244) & "ng" Else docso = dau & Trim(docso)
Else
  docso = chuyenso
End If
End Function


'2. English
Function SpellNumber(ByVal MyNumber)

Dim Dollars, Cents, Temp

Dim DecimalPlace, Count

ReDim Place(9) As String

Place(2) = " Thousand "

Place(3) = " Million "

Place(4) = " Billion "

Place(5) = " Trillion "

' String representation of amount.

MyNumber = Trim(Str(MyNumber))

' Position of decimal place 0 if none.

DecimalPlace = InStr(MyNumber, ".")

' Convert cents and set MyNumber to dollar amount.

If DecimalPlace > 0 Then

Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))

MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))

End If

Count = 1

Do While MyNumber <> ""

Temp = GetHundreds(Right(MyNumber, 3))

If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars

If Len(MyNumber) > 3 Then

MyNumber = Left(MyNumber, Len(MyNumber) - 3)

Else

MyNumber = ""

End If

Count = Count + 1

Loop

Select Case Dollars

Case ""

Dollars = "No Dollars"

Case "One"

Dollars = "One Dollar"

Case Else

Dollars = Dollars & " Dollars"

End Select

Select Case Cents

Case ""

Cents = " and No Cents"

Case "One"

Cents = " and One Cent"

Case Else

Cents = " and " & Cents & " Cents"

End Select

SpellNumber = Dollars & Cents

End Function


' Converts a number from 100-999 into text

Function GetHundreds(ByVal MyNumber)

Dim Result As String

If Val(MyNumber) = 0 Then Exit Function

MyNumber = Right("000" & MyNumber, 3)

' Convert the hundreds place.

If Mid(MyNumber, 1, 1) <> "0" Then

Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "

End If

' Convert the tens and ones place.

If Mid(MyNumber, 2, 1) <> "0" Then

Result = Result & GetTens(Mid(MyNumber, 2))

Else

Result = Result & GetDigit(Mid(MyNumber, 3))

End If

GetHundreds = Result

End Function


' Converts a number from 10 to 99 into text.


Function GetTens(TensText)

Dim Result As String

Result = "" ' Null out the temporary function value.

If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...

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 ' If value between 20-99...

Select Case Val(Left(TensText, 1))

Case 2: Result = "Twenty "

Case 3: Result = "Thirty "

Case 4: Result = "Forty "

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)) ' Retrieve ones place.

End If

GetTens = Result

End Function


' Converts a number from 1 to 9 into text.

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

Over 50 Ebooks Excel and VBA free Download

1. Statistics and Probability for Engineering Applications With Microsoft Excel by W.J. DeCoursey - PDF Free Download Download Siz...