CHUYỂN SỐ TIỀN BẰNG SỐ SANG CHỮ BẰNG NGÔN NGỮ LẬP TRÌNH VBA

Với số tiền là “1 289 020” làm thế nào để Excel đọc được ở dạng chữ là “Mot trieu hai tram tam chin ngan hai muoi dong”.

Bài viết sẽ giúp bạn chuyển số tiền bằng số sang chữ bằng cách sử dụng ngôn ngữ lập trình VBA (Visual Basic for Applications) trên Excel. Rất hữu dụng cho những ai đã, đang và sẽ làm công việc văn phòng, kế toán…

Sau đây sẽ là hướng dẫn chi tiết:

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

vba

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 để Chuyển số tiền bằng số sang chữ:

Function TIENTE(ByVal MyNumber)

Dim Temp

Dim VND

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 VND.

Temp = ConvertHundreds(Right(MyNumber, 3))

If Temp <> “” Then VND = Temp & Place(Count) & VND

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 VND.

Select Case VND

Case “”

VND = “Khong Nghin”

Case “One”

VND = “Mot Nghin”

Case Else

VND = VND & ” Dong”

End Select

TIENTE = VND

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

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 =TIENTE(A2) để chuyển số tiền bằng số sang chữ (với ô A2 là số tiền bằng chữ số cần chuyển).

Ví du: Tại ô A2 có giá trị “33449000” thì kết quả tại ô B2 là =TIENTE(A2) trả về là “Ba ba trieu bon tram bon muoi chin ngin dong”

tiente

Chúc các bạn thành công!

Bài viết liên quan

20Th4

Cách tải video từ youtube về máy tính

Bạn là 1 người thích xem những video clip trên website youtube.com. Một trong những trang mạng xem video tốt...

20Th4

Khắc phục lỗi bàn phím khi gõ công thức trong Excel

Khắc phục lỗi bàn phím khi gõ công thức trong Excel Một số máy tính khi gõ máy tính bạn...

18Th4

Tùy chỉnh dấu (,) hoặc (;) trong công thức Excel

Khi bạn sử dung công thức, Excel sẽ hiển thị cho bạn biết các tham số trong công thức của...

14Th4

Cách đọc những công thức Excel phức tạp

Bạn thường xuyên bắt gặp hay phải thựchiện những công thức phức tạp được lồng ghép nhiều hàm vào với...

12Th4

THIẾT LẬP FONT CHỮ MẶC ĐỊNH CHO WORD 2013

Làm thể nào để thiết lập một font chữ cụ thể trong chương trình soạn thảo văn bản Word khi...

08Th4

Trung Tâm Đào Tạo Tin Học Tại Hải Phòng

Trung tâm Trí Tuệ Việt xin giới thiệu tới các bạn khóa học tin học văn phòng từ cơ bản...