Function UniVBA và MsgUni hỗ trợ nhập tiếng việt (Font Unicode) trong VBA

Liên hệ QC

hoangdanh282vn

Nguyễn Cảnh Hoàng Danh
Thành viên danh dự
Tham gia
21/12/07
Bài viết
1,901
Được thích
5,297
Nghề nghiệp
Kinh doanh các mặt hàng văn phòng phẩm
Gửi các bạn 2 hàm chuyển đổi, hỗ trợ nhập tiếng việt trực tiếp trong VBA.

1. Hàm UniVBA(Str)

UniVBA("Nguye64n Ca3nh Hoa2ng Danh") = "Nguyễn Cảnh Hoàng Danh"
PHP:
Public Function UniVBA(Str As String) As String
Dim Ma As String, MaLuu As String, i As Long, a As Long
a = 1
For i = a To Len(Str)
i = a
Ma = Mid(Str, i, 3)
MaLuu = Ma
Select Case Ma
    Case "a81": Ma = ChrW(7855):     Case "A81": Ma = ChrW(7854)
    Case "a82": Ma = ChrW(7857):     Case "A82": Ma = ChrW(7856)
    Case "a83": Ma = ChrW(7859):     Case "A83": Ma = ChrW(7858)
    Case "a84": Ma = ChrW(7861):     Case "A84": Ma = ChrW(7860)
    Case "a85": Ma = ChrW(7863):     Case "A85": Ma = ChrW(7862)
    Case "a61": Ma = ChrW(7845):     Case "A61": Ma = ChrW(7844)
    Case "a62": Ma = ChrW(7847):     Case "A62": Ma = ChrW(7846)
    Case "a63": Ma = ChrW(7849):     Case "A63": Ma = ChrW(7848)
    Case "a64": Ma = ChrW(7851):     Case "A64": Ma = ChrW(7850)
    Case "a65": Ma = ChrW(7853):     Case "A65": Ma = ChrW(7852)
    Case "e61": Ma = ChrW(7871):     Case "E61": Ma = ChrW(7870)
    Case "e62": Ma = ChrW(7873):     Case "E62": Ma = ChrW(7872)
    Case "e63": Ma = ChrW(7875):     Case "E63": Ma = ChrW(7874)
    Case "e64": Ma = ChrW(7877):     Case "E64": Ma = ChrW(7876)
    Case "e65": Ma = ChrW(7879):     Case "E65": Ma = ChrW(7878)
    Case "o61": Ma = ChrW(7889):     Case "O61": Ma = ChrW(7888)
    Case "o62": Ma = ChrW(7891):     Case "O62": Ma = ChrW(7890)
    Case "o63": Ma = ChrW(7893):     Case "O63": Ma = ChrW(7892)
    Case "o64": Ma = ChrW(7895):     Case "O64": Ma = ChrW(7894)
    Case "o65": Ma = ChrW(7897):     Case "O65": Ma = ChrW(7896)
    Case "o71": Ma = ChrW(7899):     Case "O71": Ma = ChrW(7898)
    Case "o72": Ma = ChrW(7901):     Case "O72": Ma = ChrW(7900)
    Case "o73": Ma = ChrW(7903):     Case "O73": Ma = ChrW(7902)
    Case "o74": Ma = ChrW(7905):     Case "O74": Ma = ChrW(7904)
    Case "o75": Ma = ChrW(7907):     Case "O75": Ma = ChrW(7906)
    Case "u71": Ma = ChrW(7913):     Case "U71": Ma = ChrW(7912)
    Case "u72": Ma = ChrW(7915):     Case "U72": Ma = ChrW(7914)
    Case "u73": Ma = ChrW(7917):     Case "U73": Ma = ChrW(7916)
    Case "u74": Ma = ChrW(7919):     Case "U74": Ma = ChrW(7918)
    Case "u75": Ma = ChrW(7921):     Case "U75": Ma = ChrW(7920)
End Select
If Ma <> MaLuu Then
    UniVBA = UniVBA & Ma
    a = i + 3
Else
    Ma = Mid(Str, i, 2)
    MaLuu = Ma
    Select Case Ma
        Case "a1": Ma = ChrW(225):     Case "A1": Ma = ChrW(193)
        Case "a2": Ma = ChrW(224):     Case "A2": Ma = ChrW(192)
        Case "a3": Ma = ChrW(7843):    Case "A3": Ma = ChrW(7842)
        Case "a4": Ma = ChrW(227):     Case "A4": Ma = ChrW(195)
        Case "a5": Ma = ChrW(7841):    Case "A5": Ma = ChrW(7840)
        Case "a8": Ma = ChrW(259):     Case "A8": Ma = ChrW(258)
        Case "a6": Ma = ChrW(226):     Case "A6": Ma = ChrW(194)
        Case "d9": Ma = ChrW(273):     Case "D9": Ma = ChrW(272)
        Case "e1": Ma = ChrW(233):     Case "E1": Ma = ChrW(201)
        Case "e2": Ma = ChrW(232):     Case "E2": Ma = ChrW(200)
        Case "e3": Ma = ChrW(7867):    Case "E3": Ma = ChrW(7866)
        Case "e4": Ma = ChrW(7869):    Case "E4": Ma = ChrW(7868)
        Case "e5": Ma = ChrW(7865):    Case "E5": Ma = ChrW(7864)
        Case "e6": Ma = ChrW(234):     Case "E6": Ma = ChrW(202)
        Case "i1": Ma = ChrW(237):     Case "I1": Ma = ChrW(205)
        Case "i2": Ma = ChrW(236):     Case "I2": Ma = ChrW(204)
        Case "i3": Ma = ChrW(7881):    Case "I3": Ma = ChrW(7880)
        Case "i4": Ma = ChrW(297):     Case "I4": Ma = ChrW(296)
        Case "i5": Ma = ChrW(7883):    Case "I5": Ma = ChrW(7882)
        Case "o1": Ma = ChrW(243):     Case "O1": Ma = ChrW(211)
        Case "o2": Ma = ChrW(242):     Case "O2": Ma = ChrW(210)
        Case "o3": Ma = ChrW(7887):    Case "O3": Ma = ChrW(7886)
        Case "o4": Ma = ChrW(245):     Case "O4": Ma = ChrW(213)
        Case "o5": Ma = ChrW(7885):    Case "O5": Ma = ChrW(7884)
        Case "o6": Ma = ChrW(244):     Case "O6": Ma = ChrW(212)
        Case "o7": Ma = ChrW(417):     Case "O7": Ma = ChrW(416)
        Case "u1": Ma = ChrW(250):     Case "U1": Ma = ChrW(218)
        Case "u2": Ma = ChrW(249):     Case "U2": Ma = ChrW(217)
        Case "u3": Ma = ChrW(7911):    Case "U3": Ma = ChrW(7910)
        Case "u4": Ma = ChrW(361):     Case "U4": Ma = ChrW(360)
        Case "u5": Ma = ChrW(7909):    Case "U5": Ma = ChrW(7908)
        Case "u7": Ma = ChrW(432):     Case "U7": Ma = ChrW(431)
        Case "y1": Ma = ChrW(253):     Case "Y1": Ma = ChrW(221)
        Case "y2": Ma = ChrW(7923):    Case "Y2": Ma = ChrW(7922)
        Case "y3": Ma = ChrW(7927):    Case "Y3": Ma = ChrW(7926)
        Case "y4": Ma = ChrW(7929):    Case "Y4": Ma = ChrW(7928)
        Case "y5": Ma = ChrW(7925):    Case "Y5": Ma = ChrW(7924)
    End Select
    If Ma <> MaLuu Then
        UniVBA = UniVBA & Ma
        a = i + 2
    Else
        UniVBA = UniVBA & Mid(Str, i, 1)
        a = i + 1
    End If
End If
Next i
End Function
 
Lần chỉnh sửa cuối:
Và Đây là Hàm dùng cho Telex :

UniVBAT :
Đào mộ phát. Tiếng Việt mà theo kiểu telex nó vẫn dễ nhìn hơn chrW tè le các bác ạ. Nên em đổi tí với mục đích chuyển:

Thánh thần thiên địa ơi, cuộc đời thật là nghiệt ngã lắm thay!!!
>>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>>
Thasnh thaafn thieen ddija owi, cuoojc ddowfi thaajt laf nghieejt ngax lawsm thay!!!

Nói chung em chỉ đảo lại, thử thì thấy có vẻ đúng, không biết có thừa thiếu đoạn nào không? Các bác check qua cho em cái nhé.
Mã:
Public Function VBAUniT(Str As String) As String
    Dim Ma As String, MaLuu As String, i As Long, a As Long
    a = 1
    For i = a To Len(Str)
        i = a
        Ma = Mid(Str, i, 1)
        MaLuu = Ma
        Select Case Ma
        Case ChrW(7855): Ma = "aws": Case ChrW(7854): Ma = "Aws"
        Case ChrW(7857): Ma = "awf": Case ChrW(7856): Ma = "Awf"
        Case ChrW(7859): Ma = "awr": Case ChrW(7858): Ma = "Awr"
        Case ChrW(7861): Ma = "awx": Case ChrW(7860): Ma = "Awx"
        Case ChrW(7863): Ma = "awj": Case ChrW(7862): Ma = "Awj"
        Case ChrW(7845): Ma = "aas": Case ChrW(7844): Ma = "Aas"
        Case ChrW(7847): Ma = "aaf": Case ChrW(7846): Ma = "Aaf"
        Case ChrW(7849): Ma = "aar": Case ChrW(7848): Ma = "Aar"
        Case ChrW(7851): Ma = "aax": Case ChrW(7850): Ma = "Aax"
        Case ChrW(7853): Ma = "aaj": Case ChrW(7852): Ma = "Aaj"
        Case ChrW(7871): Ma = "ees": Case ChrW(7870): Ma = "Ees"
        Case ChrW(7873): Ma = "eef": Case ChrW(7872): Ma = "Eef"
        Case ChrW(7875): Ma = "eer": Case ChrW(7874): Ma = "Eer"
        Case ChrW(7877): Ma = "eex": Case ChrW(7876): Ma = "Eex"
        Case ChrW(7879): Ma = "eej": Case ChrW(7878): Ma = "Eej"
        Case ChrW(7889): Ma = "oos": Case ChrW(7888): Ma = "Oos"
        Case ChrW(7891): Ma = "oof": Case ChrW(7890): Ma = "Oof"
        Case ChrW(7893): Ma = "oor": Case ChrW(7892): Ma = "Oor"
        Case ChrW(7895): Ma = "oox": Case ChrW(7894): Ma = "Oox"
        Case ChrW(7897): Ma = "ooj": Case ChrW(7896): Ma = "Ooj"
        Case ChrW(7899): Ma = "ows": Case ChrW(7898): Ma = "Ows"
        Case ChrW(7901): Ma = "owf": Case ChrW(7900): Ma = "Owf"
        Case ChrW(7903): Ma = "owr": Case ChrW(7902): Ma = "Owr"
        Case ChrW(7905): Ma = "owx": Case ChrW(7904): Ma = "Owx"
        Case ChrW(7907): Ma = "owj": Case ChrW(7906): Ma = "Owj"
        Case ChrW(7913): Ma = "uws": Case ChrW(7912): Ma = "Uws"
        Case ChrW(7915): Ma = "uwf": Case ChrW(7914): Ma = "Uwf"
        Case ChrW(7917): Ma = "uwr": Case ChrW(7916): Ma = "Uwr"
        Case ChrW(7919): Ma = "uwx": Case ChrW(7918): Ma = "Uwx"
        Case ChrW(7921): Ma = "uwj": Case ChrW(7920): Ma = "Uwj"
        Case ChrW(7857): Ma = "awf": Case ChrW(7856): Ma = "Awf"
        Case ChrW(225): Ma = "as": Case ChrW(193): Ma = "As"
        Case ChrW(224): Ma = "af": Case ChrW(192): Ma = "Af"
        Case ChrW(7843): Ma = "ar": Case ChrW(7842): Ma = "Ar"
        Case ChrW(227): Ma = "ax": Case ChrW(195): Ma = "Ax"
        Case ChrW(7841): Ma = "aj": Case ChrW(7840): Ma = "Aj"
        Case ChrW(259): Ma = "aw": Case ChrW(258): Ma = "Aw"
        Case ChrW(226): Ma = "aa": Case ChrW(194): Ma = "Aa"
        Case ChrW(273): Ma = "dd": Case ChrW(272): Ma = "Dd"
        Case ChrW(233): Ma = "es": Case ChrW(201): Ma = "Es"
        Case ChrW(232): Ma = "ef": Case ChrW(200): Ma = "Ef"
        Case ChrW(7867): Ma = "er": Case ChrW(7866): Ma = "Er"
        Case ChrW(7869): Ma = "ex": Case ChrW(7868): Ma = "Ex"
        Case ChrW(7865): Ma = "ej": Case ChrW(7864): Ma = "Ej"
        Case ChrW(234): Ma = "ee": Case ChrW(202): Ma = "Ee"
        Case ChrW(237): Ma = "is": Case ChrW(205): Ma = "Is"
        Case ChrW(236): Ma = "if": Case ChrW(204): Ma = "If"
        Case ChrW(7881): Ma = "ir": Case ChrW(7880): Ma = "Ir"
        Case ChrW(297): Ma = "ix": Case ChrW(296): Ma = "Ix"
        Case ChrW(7883): Ma = "ij": Case ChrW(7882): Ma = "Ij"
        Case ChrW(243): Ma = "os": Case ChrW(211): Ma = "Os"
        Case ChrW(242): Ma = "of": Case ChrW(210): Ma = "Of"
        Case ChrW(7887): Ma = "or": Case ChrW(7886): Ma = "Or"
        Case ChrW(245): Ma = "ox": Case ChrW(213): Ma = "Ox"
        Case ChrW(7885): Ma = "oj": Case ChrW(7884): Ma = "Oj"
        Case ChrW(244): Ma = "oo": Case ChrW(212): Ma = "Oo"
        Case ChrW(417): Ma = "ow": Case ChrW(416): Ma = "Ow"
        Case ChrW(250): Ma = "us": Case ChrW(218): Ma = "Us"
        Case ChrW(249): Ma = "uf": Case ChrW(217): Ma = "Uf"
        Case ChrW(7911): Ma = "ur": Case ChrW(7910): Ma = "Ur"
        Case ChrW(361): Ma = "ux": Case ChrW(360): Ma = "Ux"
        Case ChrW(7909): Ma = "uj": Case ChrW(7908): Ma = "Uj"
        Case ChrW(432): Ma = "uw": Case ChrW(431): Ma = "Uw"
        Case ChrW(253): Ma = "ys": Case ChrW(221): Ma = "Ys"
        Case ChrW(7923): Ma = "yf": Case ChrW(7922): Ma = "Yf"
        Case ChrW(7927): Ma = "yr": Case ChrW(7926): Ma = "Yr"
        Case ChrW(7929): Ma = "yx": Case ChrW(7928): Ma = "Yx"
        Case ChrW(7925): Ma = "yj": Case ChrW(7924): Ma = "Yj"
        End Select
        If Ma <> MaLuu Then
            VBAUniT = VBAUniT & Ma
            a = i + 1
        Else
            VBAUniT = VBAUniT & Mid(Str, i, 1)
            a = i + 1
        End If
    Next i
End Function
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom