Attribute VB_Name = "modDocSo" ' ===================================================================== ' Ham DocSoTien - doc so tien thanh chu tieng Viet trong Excel ' Nguon: https://docso.co/ham-excel/ (mien phi, dung tuy y) ' ' Cach dung (dau phan cach tham so la ; hoac , tuy cai dat may): ' =DocSoTien(A1) -> Mot trieu hai tram nghin dong ' =DocSoTien(A1; "") -> khong kem don vi ' =DocSoTien(A1; "dong"; TRUE) -> them chu "chan" o cuoi ' =DocSoTien(A1; "do la My") -> doi don vi ' ' Quy tac: chuan ke toan (linh, muoi lam, hai muoi tu, khong tram...), ' so le duoc lam tron den dong. Doc toi 27 chu so. ' File chi dung ky tu ASCII; chu co dau duoc tao bang ChrW nen ' khong bi loi font khi dan vao Excel. ' ===================================================================== Option Explicit Private D(0 To 9) As String Private sTram As String, sLinh As String, sMuoi1 As String, sLam As String Private sMuoi As String, sMot As String, sTu As String, sTrieu As String Private sNghin As String, sTy As String, sDong As String, sChan As String Private sAm As String, Ready As Boolean ' Giai ma chuoi co dang {XXXX} (ma Unicode hex) thanh chu co dau Private Function U(ByVal s As String) As String Dim i As Long, r As String i = 1 Do While i <= Len(s) If Mid$(s, i, 1) = "{" And Mid$(s, i + 5, 1) = "}" Then r = r & ChrW(CLng("&H" & Mid$(s, i + 1, 4))) i = i + 6 Else r = r & Mid$(s, i, 1) i = i + 1 End If Loop U = r End Function Private Sub Init() Dim a As Variant, i As Long If Ready Then Exit Sub a = Split(U("kh{00F4}ng,m{1ED9}t,hai,ba,b{1ED1}n,n{0103}m,s{00E1}u,b{1EA3}y,t{00E1}m,ch{00ED}n"), ",") For i = 0 To 9 D(i) = a(i) Next i sTram = U("tr{0103}m") sLinh = "linh" sMuoi1 = U("m{01B0}{1EDD}i") sLam = U("l{0103}m") sMuoi = U("m{01B0}{01A1}i") sMot = U("m{1ED1}t") sTu = U("t{01B0}") sTrieu = U("tri{1EC7}u") sNghin = U("ngh{00EC}n") sTy = U("t{1EF7}") sDong = U("{0111}{1ED3}ng") sChan = U("ch{1EB5}n") sAm = U("{00E2}m") Ready = True End Sub ' Doc 1 nhom 3 chu so (0..999). inner = nhom nam giua (doc "khong tram") Private Function Triple(ByVal n As Long, ByVal inner As Boolean) As String Dim h As Long, t As Long, v As Long, r As String, said As Boolean h = n \ 100 t = (n \ 10) Mod 10 v = n Mod 10 If h > 0 Or inner Then r = D(h) & " " & sTram said = True End If If t = 0 Then If v > 0 Then If said Or inner Then r = r & " " & sLinh r = r & " " & D(v) End If ElseIf t = 1 Then r = r & " " & sMuoi1 If v = 5 Then r = r & " " & sLam ElseIf v > 0 Then r = r & " " & D(v) End If Else r = r & " " & D(t) & " " & sMuoi If v = 1 Then r = r & " " & sMot ElseIf v = 4 Then r = r & " " & sTu ElseIf v = 5 Then r = r & " " & sLam ElseIf v > 0 Then r = r & " " & D(v) End If End If Triple = Trim$(r) End Function ' Doc so nho hon 1 ty (0..999 999 999) Private Function BelowBillion(ByVal n As Long, ByVal inner As Boolean) As String Dim g(0 To 2) As Long, i As Long, r As String, started As Boolean g(0) = n \ 1000000 g(1) = (n \ 1000) Mod 1000 g(2) = n Mod 1000 For i = 0 To 2 If g(i) <> 0 Then r = r & " " & Triple(g(i), started Or inner) If i = 0 Then r = r & " " & sTrieu If i = 1 Then r = r & " " & sNghin started = True End If Next i BelowBillion = Trim$(r) End Function ' Doc chuoi chu so (khong co so 0 o dau), cat tung khuc 9 chu so theo "ty" Private Function IntDigits(ByVal s As String, ByVal inner As Boolean) As String Dim hi As String, lo As Long, r As String If Len(s) > 9 Then hi = Left$(s, Len(s) - 9) lo = CLng(Right$(s, 9)) r = IntDigits(hi, inner) & " " & sTy If lo > 0 Then r = r & " " & BelowBillion(lo, True) IntDigits = r Else IntDigits = BelowBillion(CLng(s), inner) End If End Function ' Kiem tra chuoi "1.250.000" / "1,250,000" / "1250000" Private Function CleanText(ByVal s As String, ByRef neg As Boolean) As String Dim p As Variant, i As Long, sep As String s = Replace(Replace(Trim$(s), " ", ""), ChrW(160), "") If Left$(s, 1) = "-" Then neg = True s = Mid$(s, 2) End If If InStr(s, ".") > 0 Then sep = "." If InStr(s, ",") > 0 Then If sep <> "" Then Exit Function sep = "," End If If sep <> "" Then p = Split(s, sep) If Len(p(0)) < 1 Or Len(p(0)) > 3 Then Exit Function For i = 1 To UBound(p) If Len(p(i)) <> 3 Then Exit Function Next i s = Replace(s, sep, "") End If If s = "" Or s Like "*[!0-9]*" Then Exit Function CleanText = s End Function ' =DocSoTien(SoTien; [DonVi]; [Chan]) Public Function DocSoTien(ByVal SoTien As Variant, Optional ByVal DonVi As Variant, _ Optional ByVal Chan As Boolean = False) As Variant Dim s As String, x As Variant, neg As Boolean, r As String, dv As String Init If IsObject(SoTien) Then SoTien = SoTien.Value If IsEmpty(SoTien) Then DocSoTien = "" Exit Function End If If VarType(SoTien) = vbString Then If Trim$(SoTien) = "" Then DocSoTien = "" Exit Function End If s = CleanText(CStr(SoTien), neg) ElseIf IsNumeric(SoTien) And VarType(SoTien) <> vbBoolean Then x = CDec(SoTien) If x < 0 Then neg = True x = -x End If x = Fix(x + CDec(1) / 2) s = CStr(x) End If If s = "" Then DocSoTien = CVErr(xlErrValue) Exit Function End If Do While Len(s) > 1 And Left$(s, 1) = "0" s = Mid$(s, 2) Loop If Len(s) > 27 Then DocSoTien = CVErr(xlErrNum) Exit Function End If If s = "0" Then r = D(0) Else r = IntDigits(s, False) If neg Then r = sAm & " " & r End If If IsMissing(DonVi) Then dv = sDong Else dv = Trim$(CStr(DonVi)) End If If dv <> "" Then r = r & " " & dv If Chan Then r = r & " " & sChan End If DocSoTien = UCase$(Left$(r, 1)) & Mid$(r, 2) End Function