Lưu trữ hàng tháng: Tháng 1 2017

Code tach 1 sheet tổng hợp ra cách sheet khac trong cùng 1 file

Sub TachNhom()

Dim myCell As Range
    Dim wks As Worksheet
    Dim DataBaseWks As Worksheet
    Dim ListRange As Range
    Dim dummyRng As Range
    Dim myDatabase As Range
    Dim TempWks As Worksheet
    Dim rsp As Integer
    Dim i As Long

    Const TopLeftCellOfDataBase As String = “A4”
    Const KeyColumn As String = “C”
    ‘muon tach cot nao thi dien ten cot do
 

    Set DataBaseWks = Worksheets(“DATA”)
    i = DataBaseWks.Range(TopLeftCellOfDataBase).Row – 1

    Set TempWks = Worksheets.Add

    With DataBaseWks
        Set dummyRng = .UsedRange
        Set myDatabase = .Range(TopLeftCellOfDataBase, _
                            .Cells.SpecialCells(xlCellTypeLastCell))
    End With

    With DataBaseWks
        Intersect(myDatabase, .Columns(KeyColumn)).AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=TempWks.Range(“A1”), _
            Unique:=True

        TempWks.Range(“D1“).Value = _
            .Cells(.Range(TopLeftCellOfDataBase).Row, KeyColumn).Value
    End With

    With TempWks
        Set ListRange = .Range(“A2”, .Cells(.Rows.Count, “A”).End(xlUp))
    End With

    With ListRange
        .Sort Key1:=.Cells(1), Order1:=xlAscending, _
            Header:=xlNo, OrderCustom:=1, _
            MatchCase:=False, Orientation:=xlTopToBottom
    End With

    For Each myCell In ListRange.Cells
        If WksExists(myCell.Value) = False Then
            Set wks = Sheets.Add
            On Error Resume Next
            wks.Name = myCell.Value
            If Err.Number <> 0 Then
                MsgBox “Please rename: ” & wks.Name
                Err.Clear
             
            End If
            On Error GoTo 0
            wks.Move After:=Sheets(Sheets.Count)
        Else
            Set wks = Worksheets(myCell.Value)
            wks.Cells.Clear
        End If

        If rsp = 6 Then
          DataBaseWks.Rows(“1:” & i).Copy Destination:=wks.Range(“A1”)
        End If
     
        TempWks.Range(“D2”).Value = “=” & Chr(34) & “=” & myCell.Value & Chr(34)

        If rsp = 6 Then
          myDatabase.AdvancedFilter _
              Action:=xlFilterCopy, _
              CriteriaRange:=TempWks.Range(“D1:D2”), _
              CopyToRange:=wks.Range(“A1”).Offset(i, 0), _
              Unique:=False
        Else
          myDatabase.AdvancedFilter _
              Action:=xlFilterCopy, _
              CriteriaRange:=TempWks.Range(“D1:D2”), _
              CopyToRange:=wks.Range(“A1”), _
              Unique:=False
              Columns(“D:D”).ColumnWidth = 20
              Columns(“F:F”).ColumnWidth = 20
              Columns(“G:G”).ColumnWidth = 15
        End If
     
    Next myCell
 
    Application.DisplayAlerts = False
    TempWks.Delete
    Application.DisplayAlerts = True

    MsgBox “TÁCH  THÀNH  CÔNG”

End Sub
Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function

———————

code tách các sheet ra thành nhiều file tại 1 forder

Sub tachsheet()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sh As Worksheet
For Each sh In Worksheets
sh.Copy
ActiveWorkbook.SaveAs ThisWorkbook.Path & "" & sh.Name, 51
' Có thể đổi lại vị trí lư tại chỗ ""
ActiveWorkbook.Close
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

Code gộp file – gộp sheet các kiểu trong excel

Code tìm các file dữ liệu cần gộp
Sub GopfileTH()
    Dim FilesToOpen
    Dim x As Integer
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
    FilesToOpen = Application.GetOpenFilename _
    (FileFilter:=”Microsoft Excel Files (*.xls*), *.xls*”, MultiSelect:=True, Title:=”Files to Merge”)
    If TypeName(FilesToOpen) = “Boolean” Then
        MsgBox “Khong co file nao duoc chon”
        GoTo ExitHandler
    End If
    x = 1
    While x <= UBound(FilesToOpen)
        Workbooks.Open Filename:=FilesToOpen(x)
        Sheets().Move After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        x = x + 1
    Wend
ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub

Code gộp file vừa chọn
Sub Gopfile()
    Dim FilesToOpen
    Dim Filetonghopmoi As Workbook
    Dim filedangmo As Workbook
    Dim strFilename As String
    Dim x As Integer
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
    FilesToOpen = Application.GetOpenFilename _
    (FileFilter:=”Microsoft Excel Files (*.xls*), *.xls*”, MultiSelect:=True, Title:=”Files to Merge”)
    If TypeName(FilesToOpen) = “Boolean” Then
        MsgBox “Khong co file nao duoc chon”
        GoTo ExitHandler
    End If
   
‘=====
‘Tao file tong hop moi noi luu mac dinh ngoai desktop

    Set Filetonghopmoi = Workbooks.Add(xlWBATWorksheet)
    strFilename = Dir(“C:UsersAdministratorDesktop” & “*.xls”, vbNormal)
   
 
    x = 1
    While x <= UBound(FilesToOpen)
    Set filedangmo = Workbooks.Open(Filename:=FilesToOpen(x))
    Sheets(“PL”).Copy After:=Filetonghopmoi.Sheets(Filetonghopmoi.Sheets.Count)
    ‘Thay ten sheet can gop cho phu hop. o day dang gop tat ca cac sheet co ten “PL” o ca file da cho
    Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    filedangmo.Close False
   
    x = x + 1
    Wend

ExitHandler:
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub

———
Code gộp sheet trong file
Option Explicit

‘ Tao chuc nang tim ra DONG cuoi cung

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:=”*”, After:=sh.Range(“A1”), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
On Error GoTo 0
End Function

‘ Tao chuc nang tim ra COT cuoi cung

Function Lastcol(sh As Worksheet)
On Error Resume Next
Lastcol = sh.Cells.Find(What:=”*”, After:=sh.Range(“A1”), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
On Error GoTo 0
End Function

‘ Tao lenh GOPSHEET

Sub gopsheet()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

‘ XÓA bo sheet TONGHOP neu no da duoc thiet lap

Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets(“TONGHOP”).Delete
On Error GoTo 0
Application.DisplayAlerts = True

‘ TAO 1 sheet moi ten TONGHOP

Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = “TONGHOP”

‘Nhap dong bat dau

StartRow = 2

‘ Chay qua tat cac cac sheet  va thuc hien copy du lieu vao sheet TONGHOP

For Each sh In ActiveWorkbook.Worksheets
   If sh.Name <> DestSh.Name Then
 
‘ Tim don cuoi cung trong sheet tong hop va sheet thanh phan

   Last = LastRow(DestSh)
   shLast = LastRow(sh)
 

‘ Thuc hien copy du kieu khi sheet du lieu KHONG RONG va dong CUOI > dong BAT DAU
           If shLast > 0 And shLast >= StartRow Then
             
‘ thiet lap VUNG can copy
           
            Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))

‘ Kiem tra xem sheet TONGHOP co du dong de copy sang khong

               If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
                  MsgBox “Khong du dong trong bang TONGHOP de copy sang”
                  GoTo ExitTheSub
               End If

‘ Copy du lieu o dang GIA TRI
             
               CopyRng.Copy
               With DestSh.Cells(Last + 1, “A”)
                   .PasteSpecial xlPasteValues
                   .PasteSpecial xlPasteFormats
                   Application.CutCopyMode = False
               End With

           End If

       End If
 
   Next

ExitTheSub:

   Application.GoTo DestSh.Cells(1)

   ‘ Tu dong dieu chinh so cot theo sheet TONGHOP
 
 
   DestSh.Columns.AutoFit

   With Application
       .ScreenUpdating = True
       .EnableEvents = True
   End With
End Sub

————-
Code xóa hết các sheet vừa thêm
‘————————-
Sub XoaSheetVuaThem()

Application.DisplayAlerts = False
Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name <> ActiveSheet.Name Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True

End Sub

Code đổi số sang chữ trong excel

Tải Add-in về để cài vào máy: Account Help 
Thiết lập cài Add-in với từng phiên bản office khác nhau 2003 và 2007
Sử dụng:
gõ hàm 
=VND(giá trị số, loại font(1/2/3-mặc định3), “loại đơn vị tiền tệ”,”giá trị số lẻ”) để đổi tiếng việt
= USD để đổi tiếng anh

VD: A1=30,5
A2=VND(A1,,”USD”,”cent”) --> ba mươi USD năm mươi CENT



Ngoài ra có thể tham khảo code của chuyển đổi font khác ở bên dưới


Public Function VND(BaoNhieu)
If Val(BaoNhieu) = 0 Then
Ketqua = “Khoâng ñoàng”
Else
If Abs(BaoNhieu) > 1E+15 Then
Ketqua = “Soá quaù lôùn”
Else
If BaoNhieu < 0 Then Ketqua = “Tröø” & Space(1) Else Ketqua = Space(0)
SOTIEN = Format(Abs(BaoNhieu), “###############0.00”) ’18coät soá 2soá leû
SOTIEN = Right(Space(15) & SOTIEN, 18-)
Hang = Array(“None”, “traêm”, “möôi”, “gì ñoù”)
DONVI = Array(“None”, “ngaøn tyû”, “tyû”, “trieäu”, “ngaøn”, “ñoàng”, “xu”)
Dem = Array(“None”, “moât”, “hai”, “ba”, “boán”, “naêm”, “saùu”, “baûy”, “taùm”, “chín”)
For N = 1 To 6
Nhom = Mid(SOTIEN, N * 3 – 2, 3)
If Nhom <> Space(3) Then
Select Case Nhom
Case “000”
If N = 5 Then
Chu = “ñoàng” & Space(1)
Else
Chu = Space(0)
End If
Case “.00”, “,00”
Chu = “chaün”
Case Else
S1 = Left(Nhom, 1): S2 = Mid(Nhom, 2, 1): S3 = Right(Nhom, 1)
Chu = Space(0): Hang(3) = DONVI(N)
For K = 1 To 3
Dich = Space(0): S = Val(Mid(Nhom, K, 1))
If S > 0 Then
Dich = Dem(S) & Space(1) & Hang(K) & Space(1)
Else
If K = 1 And N > 1 And N < 6 And Val(Mid(SOTIEN, (N – 1) * 3 – 2, 3)) > 0 Then
Dich = “khoâng” & Space(1) & Hang(K) & Space(1)
End If
End If
Select Case K
Case 2 And S = 1
Dich = “möôøi” & Space(1)
Case 3 And S = 0 And Nhom <> Space(2) & “0”
Dich = Hang(K) & Space(1)
Case 3 And S = 5 And Val(S2) > 2
Dich = “l” & Mid(Dich, 2)
Case 2 And S = 0 And S3 <> “0”
If N > 1 And Val(Mid(SOTIEN, (N – 1) * 3 – 2, 3)) > 0 Or (Val(S1) > 0) Then
Dich = “leû” & Space(1)
End If
End Select
Chu = Chu & Dich
Next K
End Select
ViTri = InStr(1, Chu, “möôi moät”)
If ViTri > 0 Then Mid(Chu, ViTri, 9) = “möôi moát”
Ketqua = Ketqua & Chu
End If
Next N
End If
End If
VND = UCase(Left(Ketqua, 1)) & Trim(Mid(Ketqua, 2))
End Function

HÀM ĐỔI SỐ SANG CHỮ
 
Option Explicit
Function VND(conso) As String
Dim S09, Lop3, Dau, Vt, S123, S1, S2, S3, LOP2, CHR1, CHR2, CHR3 As String
Dim Sonhan, Sochuso, I, Docso, Lop, N1, N2, N3, Baso As Long
‘………………………….SO TU 1 DEN 9 ………………………………………………….
 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”)
‘…………………………..CHUOI NGHIN -TRIEU-TY………………………………………………………………………………………….
      If Right(conso, 3) = “000” Or Len(conso) >= 13 Then Lop3 = Array(“”, ” Tri” & ChrW(7879) & “u”, ” Ngh” & ChrW(236) & “n”, ” T” & ChrW(7927) & “”) Else _
                                                          Lop3 = Array(“”, ” Tri” & ChrW(7879) & “u,”, ” Ngh” & ChrW(236) & “n,”, ” T” & ChrW(7927) & “,”)
‘…………………………..NGHIN TY-MUOI NGHIN TY-TRAM NGHIN TY……………………………………………………………………………..Or Len(conso) >= 13
   ‘If conso = 10 ^ 12 Or conso = 10 ^ 13 Or conso = 10 ^ 14 Or conso = 10 ^ 15 Then LOP2 = ” T” & ChrW(7927) & ” ” Else LOP2 = ” T” & ChrW(7927) & “,”
If Abs(Right(conso, 11)) > 0 Then LOP2 = ” T” & ChrW(7927) & “, ” Else LOP2 = ” T” & ChrW(7927) & “”
‘…………………………………………………………………………………………………………………………………………..
If Abs(conso) > 999999999999999# Then
        MsgBox “SO QUÁ LON”
        VND = “SO QUÁ LON”
        Exit Function
    End If
If Trim(conso) = “” Then
  VND = “”
ElseIf IsNumeric(conso) = True Then
  If conso < 0 Then Dau = ChrW(226) & “m ” Else Dau = “”
  conso = Application.WorksheetFunction.Round(Abs(conso), 0)
  conso = ” ” & conso
  conso = Replace(conso, “,”, “”, 1)
  Vt = InStr(1, conso, “E”)
  If Vt > 0 Then
    Sonhan = Val(Mid(conso, Vt + 1))
    conso = Trim(Mid(conso, 2, Vt – 2))
    conso = conso & String(Sonhan – Len(conso) + 1, “0”)
  End If
  conso = Trim(conso)
  Sochuso = Len(conso) Mod 9
  If Sochuso > 0 Then conso = String(9 – (Sochuso Mod 12), “0”) & conso
  Docso = “”
  I = 1
  Lop = 1
  Do
    N1 = Mid(conso, I, 1)
    N2 = Mid(conso, I + 1, 1)
    N3 = Mid(conso, I + 2, 1)
    Baso = Mid(conso, I, 3)
    I = I + 3
    If N1 & N2 & N3 = “000” Then
      If Docso <> “” And Lop = 3 And Len(conso) – I > 2 Then S123 = LOP2 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 = ” L” & ChrW(7867)
        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(conso) 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(conso) Then Exit Do
 
 
  Loop
  If Docso = “” Then VND = “Kh” & ChrW(244) & “ng” Else VND = Dau & Trim(Docso) & ” ” & ChrW(272) & ChrW(7891) & “ng.”
Else
  VND = conso
End If
End Function
 
 
 
 
 
 

Vấn đề gõ tiếng việt trong lập trình VBA

VBA chưa hỗ trợ với bộ Unicode, Các ký tự hỗ trợ trong bảng mã Unicode mới chỉ được từ 0- 225 thôi. Nên các ký tự có mã unicode nhiều hơn sẽ hiển thị dấu (? )
Bởi vậy, để có thể gõ được tiếng việt trong VBA ta thực hiện theo 1 số cách thủ công như sau:
1. Gõ nội dung vào 1 ô trong bảng tính ( VD gõ vào ô A2) Thực hiện lệnh truy xuất nội dung của ô A2: Cells(1,2) = Sheets(“Sheet1”).cells(1,2).  Nhược điểm là các ô trong sheet phải có dữ liệu. Nếu vô tình xóa đi thì dữ liệu cũng sẽ mất.

2: Sử dụng phép nối chuỗi và hàm Chrw để viết code. VD: Viết chữ Lập trình = “L”&chrw(7853)&”p trình”. Đối với cách này mình phải nhớ và biết được bảng mã unicode cuả từng ký tự. Có thể dựa vào Bảng mã để tham chiếu

3. Sử dụng 1 hàm tự tạo (UniVBa) để dịch lại nội dung gõ trong sheet ra bảng Unicode. Từ đó nhập bản dịch vào bảng lập trình ( quay trở lại cách 2).
Đoạn code dịch như sau:

Function UniVba(TxtUni As String) As String
If TxtUni = “” Then
  UniVba = “”””””
Else
  TxtUni = TxtUni & ” “
  If AscW(Left(TxtUni, 1)) < 256 Then UniVba = “”””
  For n = 1 To Len(TxtUni) – 1
    uni1 = Mid(TxtUni, n, 1)
    uni2 = AscW(Mid(TxtUni, n + 1, 1))
    If AscW(uni1) > 255 And uni2 > 255 Then
      UniVba = UniVba & “ChrW(” & AscW(uni1) & “) & “
    ElseIf AscW(uni1) > 255 And uni2 < 256 Then
      UniVba = UniVba & “ChrW(” & AscW(uni1) & “) & “””
    ElseIf AscW(uni1) < 256 And uni2 > 255 Then
      UniVba = UniVba & uni1 & “”” & “
    Else
      UniVba = UniVba & uni1
    End If
  Next
  If Right(UniVba, 4) = ” & “”” Then
    UniVba = Mid(UniVba, 1, Len(UniVba) – 4)
  Else
    UniVba = UniVba & “”””
  End If
End If
End Function

Đánh dấu các giá trị khác nhau với nhiều màu khác nhau – code VB

Sub Highlight_Duplicate()
Dim ws As Worksheet
Dim cell As Range
Dim myrng As Range
Dim clr As Long
Dim lastcell As Range
Dim i As Long
Dim lastrow As Long
Set ws = ThisWorkbook.ActiveSheet
‘Vung can danh dau gia tri trung nhau
Set myrng = ws.Range(“A1:K” & Range(“C” & ws.Rows.Count).End(xlUp).Row)
With myrng
Set lastcell = .Cells(.Cells.Count)
End With
myrng.Interior.ColorIndex = xlNone
clr = 3
For Each cell In myrng
‘Kiem tra so gia tri trung nhau trong vung, neu co hai gia tri trung nhau tro len thi thuc hien
If Application.WorksheetFunction.CountIf(myrng, cell) > 1 Then
‘Neu la o dau tien cua cac gia tri trung nhau trong vung
If myrng.Find(what:=cell, lookat:=xlWhole, MatchCase:=False, after:=lastcell).Address = cell.Address Then
‘Thiet lap mau
cell.Interior.ColorIndex = clr
clr = clr + 1
i = i + 1
Else
‘Thiet lap mau tu o thu 2 voi cac gia tri trung nhau
cell.Interior.ColorIndex = myrng.Find(what:=cell, lookat:=xlWhole, MatchCase:=False, after:=lastcell).Interior.ColorIndex
End If
End If
Next
‘Lay dong cuoi cung cua vung du lieu
lastrow = Cells(Rows.Count, “A”).End(xlUp).Row
Range(“A” & lastrow + 2).Value = “Tong so co ” & i & ” gia tri trung nhau”
End Sub

Code đổi số sang chữ trong excel và viết hoa đầu câu

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



--------------------
Function viethoa(ByVal strContent As String) As String
Dim m As Object
strContent = LCase(strContent)
strContent = Application.Replace(strContent, 1, 1, UCase(Left$(strContent, 1)))
With CreateObject("VBScript.RegExp")
.Pattern = ".s."
.Global = True
For Each m In .Execute(strContent)
strContent = Application.Replace(strContent, m.FirstIndex + 1, m.Length, UCase(m.Value))
Next
End With
viethoa = strContent
End Function