
Tình huống hàng ngày: Mình có một thắc mắc nhờ các bạn chỉ giùm! Đó là trong Excel làm sao để ghi tiền bằng chữ giống như trong các hóa đơn. VD: Khi có tiền bằng số là 123.456.789 đồng làm sao để ghi ra là một trăm hai mươi triệu bốn trăm năm mươi sáu ngàn bảy trăm tám mươi chín đồng.
Giải pháp:
Bước 1. Mở tập tin cần chuyển >> Nhấn tổ hợp phím Alt + F11 để mở trình soạn thảo VBA của Excell
Bước 2. Nhấp chuột phải lên VBA Project >> Insert >> Module >> và dán đoạn mã bên dưới vào cửa sổ của Module mới chèn
Function ConvertCurrencyToVietnamese(ByVal MyNumber)
Dim Temp
Dim Dollars, Cents
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " Nghin "
Place(3) = " Trieu "
Place(4) = " Ty "
Place(5) = " Ngan ty "
' Convert MyNumber to a string, trimming extra spaces.
MyNumber = Trim(Str(MyNumber))
' Find decimal place.
DecimalPlace = InStr(MyNumber, ".")
' If we find decimal place...
If DecimalPlace > 0 Then
' Convert cents
Temp = Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)
Cents = ConvertTens(Temp)
' Strip off cents from remainder to convert.
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
' Convert last 3 digits of MyNumber to English dollars.
Temp = ConvertHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
' Remove last 3 converted digits from MyNumber.
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
' Clean up dollars.
Select Case Dollars
Case ""
Dollars = "khong Nghin"
Case "One"
Dollars = "Mot Nghin"
Case Else
Dollars = Dollars & " Nghin"
End Select
' Clean up cents.
Select Case Cents
Case ""
Cents = " va khong Dong"
Case "One"
Cents = " va mot Dong"
Case Else
Cents = " va " & Cents & " Dong"
End Select
ConvertCurrencyToVietnamese = Dollars & Cents
End Function
Private Function ConvertHundreds(ByVal MyNumber)
Dim Result As String
' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then Exit Function
' Append leading zeros to number.
MyNumber = Right("000" & MyNumber, 3)
' Do we have a hundreds place digit to convert?
If Left(MyNumber, 1) <> "0" Then
Result = ConvertDigit(Left(MyNumber, 1)) & " Tram "
End If
' Do we have a tens place digit to convert?
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & ConvertTens(Mid(MyNumber, 2))
Else
' If not, then convert the ones place digit.
Result = Result & ConvertDigit(Mid(MyNumber, 3))
End If
ConvertHundreds = Trim(Result)
End Function
Private Function ConvertTens(ByVal MyTens)
Dim Result As String
' Is value between 10 and 19?
If Val(Left(MyTens, 1)) = 1 Then
Select Case Val(MyTens)
Case 10: Result = "Muoi"
Case 11: Result = "Muoi mot"
Case 12: Result = "Muoi hai"
Case 13: Result = "Muoi ba"
Case 14: Result = "Muoi bon"
Case 15: Result = "Muoi lam"
Case 16: Result = "Moi sau"
Case 17: Result = "Muoi bay"
Case 18: Result = "Muoi tam"
Case 19: Result = "Muoi chin"
Case Else
End Select
Else
' .. otherwise it's between 20 and 99.
Select Case Val(Left(MyTens, 1))
Case 2: Result = "Hai muoi "
Case 3: Result = "Ba muoi "
Case 4: Result = "Bon muoi "
Case 5: Result = "Nam muoi "
Case 6: Result = "Sau muoi "
Case 7: Result = "Bay muoi "
Case 8: Result = "Tam muoi "
Case 9: Result = "Chin muoi "
Case Else
End Select
' Convert ones place digit.
Result = Result & ConvertDigit(Right(MyTens, 1))
End If
ConvertTens = Result
End Function
Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
Case 1: ConvertDigit = "Mot"
Case 2: ConvertDigit = "Hai"
Case 3: ConvertDigit = "Ba"
Case 4: ConvertDigit = "Bon"
Case 5: ConvertDigit = "Nam"
Case 6: ConvertDigit = "Sau"
Case 7: ConvertDigit = "Bay"
Case 8: ConvertDigit = "Tam"
Case 9: ConvertDigit = "Chin"
Case Else: ConvertDigit = ""
End Select
End Function
Dim Temp
Dim Dollars, Cents
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " Nghin "
Place(3) = " Trieu "
Place(4) = " Ty "
Place(5) = " Ngan ty "
' Convert MyNumber to a string, trimming extra spaces.
MyNumber = Trim(Str(MyNumber))
' Find decimal place.
DecimalPlace = InStr(MyNumber, ".")
' If we find decimal place...
If DecimalPlace > 0 Then
' Convert cents
Temp = Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)
Cents = ConvertTens(Temp)
' Strip off cents from remainder to convert.
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
' Convert last 3 digits of MyNumber to English dollars.
Temp = ConvertHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
' Remove last 3 converted digits from MyNumber.
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
' Clean up dollars.
Select Case Dollars
Case ""
Dollars = "khong Nghin"
Case "One"
Dollars = "Mot Nghin"
Case Else
Dollars = Dollars & " Nghin"
End Select
' Clean up cents.
Select Case Cents
Case ""
Cents = " va khong Dong"
Case "One"
Cents = " va mot Dong"
Case Else
Cents = " va " & Cents & " Dong"
End Select
ConvertCurrencyToVietnamese = Dollars & Cents
End Function
Private Function ConvertHundreds(ByVal MyNumber)
Dim Result As String
' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then Exit Function
' Append leading zeros to number.
MyNumber = Right("000" & MyNumber, 3)
' Do we have a hundreds place digit to convert?
If Left(MyNumber, 1) <> "0" Then
Result = ConvertDigit(Left(MyNumber, 1)) & " Tram "
End If
' Do we have a tens place digit to convert?
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & ConvertTens(Mid(MyNumber, 2))
Else
' If not, then convert the ones place digit.
Result = Result & ConvertDigit(Mid(MyNumber, 3))
End If
ConvertHundreds = Trim(Result)
End Function
Private Function ConvertTens(ByVal MyTens)
Dim Result As String
' Is value between 10 and 19?
If Val(Left(MyTens, 1)) = 1 Then
Select Case Val(MyTens)
Case 10: Result = "Muoi"
Case 11: Result = "Muoi mot"
Case 12: Result = "Muoi hai"
Case 13: Result = "Muoi ba"
Case 14: Result = "Muoi bon"
Case 15: Result = "Muoi lam"
Case 16: Result = "Moi sau"
Case 17: Result = "Muoi bay"
Case 18: Result = "Muoi tam"
Case 19: Result = "Muoi chin"
Case Else
End Select
Else
' .. otherwise it's between 20 and 99.
Select Case Val(Left(MyTens, 1))
Case 2: Result = "Hai muoi "
Case 3: Result = "Ba muoi "
Case 4: Result = "Bon muoi "
Case 5: Result = "Nam muoi "
Case 6: Result = "Sau muoi "
Case 7: Result = "Bay muoi "
Case 8: Result = "Tam muoi "
Case 9: Result = "Chin muoi "
Case Else
End Select
' Convert ones place digit.
Result = Result & ConvertDigit(Right(MyTens, 1))
End If
ConvertTens = Result
End Function
Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
Case 1: ConvertDigit = "Mot"
Case 2: ConvertDigit = "Hai"
Case 3: ConvertDigit = "Ba"
Case 4: ConvertDigit = "Bon"
Case 5: ConvertDigit = "Nam"
Case 6: ConvertDigit = "Sau"
Case 7: ConvertDigit = "Bay"
Case 8: ConvertDigit = "Tam"
Case 9: ConvertDigit = "Chin"
Case Else: ConvertDigit = ""
End Select
End Function
[code sưu tầm]
Bước 3. Nhấn phím Alt + F11 một lần nữa và nhấn Ctrl + S để save lại toàn bộ tài liệu.
Bước 4. Đến đây, bạn có thể sử dụng công thức =ConvertCurrencyToVietnamese(B3) để chuyển đổi tiền tệ từ số về chữ (với B3 là số tiền bằng chữ số)
Ví dụ: B3 có giá trị là: 123456 thì kết quả =ConvertCurrencyToVietnamese(B3) trả về là Mot Tram Hai muoi Ba Nghin Bon Tram Nam muoi Sau Nghin va khong Dong
10 comments:
Được, nhưng có 3 điểm nên sửa lại:
1) Bỏ cái đuôi "và không đồng" đi. Mình xài tiền Việt chứ phải US$ đâu mà có cent!
2) Thêm dấu tiếng Việt vào!
3) Chữ hoa, thường lẫn lộn kìa!
rất có ích. nhưng làm mãi không được, khó quá.
MÌNH K LÀM DC.K BIẾT MACRO LÀ GÌ CẢ
Giá trị trả về chưa đúng,
VD: 123456 Kết quả trả về là: Mot Tram Hai muoi Ba Nghin Bon Tram Nam muoi Sau Nghin va khong Dong,,
Kết quả đúng phải là: Mot Tram Hai muoi Ba Nghin Bon Tram Nam muoi Sau va khong Dong
tạm được nhưng phải bổ xung dấu phẩy nữa thì hay hơn.
hay đó bạn à.
nhưng mà liên quan đến tiền mà không có dấu thì không ổn rồi. bạn có cách nào chuyển sang tiếng Việt không?
minh chua thu ham nay nhung thay thu thuat nay rat hay
cam on ban
Kiem tra lai thay khong dep lam , khi dung co dau thi chi fonts Unicode la duoc
Rất hay nhưng ko co dấu thì ko ổn lắm, co cách nao ko ban. mình dùng font Unicode rồi mà vẫn ko dùng tiếng việt dc
Không có dấu thì không ổn lắm và bỏ đi không đồng thì mới chuẩn.
Bạn nghiên cứu thêm và có gì chỉnh sửa báo giúp với nhé
Chân thành cảm ơn.
Post a Comment