Hiển thị MsgBox chữ có dấu tiếng việt - Unicode
Trong lập trình VBA, để hiển thị thông báo ta dùng hàm MsgBox, tuy nhiên hàm này không thể hiển thị chữ tiếng Việt có dấu -unicode. Bài viết này tôi xin hướng dẫn cách tạo ra một hàm MsgBox có khả năng hiển thị chữ tiếng Việt có dấu, unicode.
Trong môi trường VBA, tạo một module mới, copy đoạn code dưới đây:
[gpecode=vb]
'Khai bao cac ham API trong thu vien User32.DLL. Chạy trong môi trường Office 32 hoặc 64-bit
#If VBA7 Then
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function MessageBoxW Lib "user32" (ByVal hwnd As LongPtr, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
#Else
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function MessageBoxW Lib "user32" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
#End If[/gpecode]
[gpecode=vb]Function MsgBoxUni(ByVal PromptUni As Variant, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal TitleUni As Variant = vbNullString) As VbMsgBoxResult
'Function MsgBoxUni(ByVal PromptUni As Variant, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal TitleUni As Variant, Optional HelpFile, Optional Context) As VbMsgBoxResult
'BStrMsg,BStrTitle : La chuoi Unicode
Dim BStrMsg, BStrTitle
'Hàm StrConv Chuyen chuoi ve ma Unicode
BStrMsg = StrConv(PromptUni, vbUnicode)
BStrTitle = StrConv(TitleUni, vbUnicode)
MsgBoxUni = MessageBoxW(GetActiveWindow, BStrMsg, BStrTitle, Buttons)
End Function[/gpecode]
[TABLE="class: cms_table_redactor_format_table"]
[TR]
[TD]'****************************************
'Tac gia: Nguyen Duy Tuan
'Tel : 0904.210.337
'E.Mail :
tuanktcdcn@yahoo.com
'Website: www.bluesofts.net
'****************************************
[TABLE="width: 1444"]
[TR]
[TD="class: gutter"]
[/TD]
[TD="class: code"][/TD]
[/TR]
[/TABLE]
[/TD]
[/TR]
[/TABLE]
XEM CHI TIẾT TẠI ĐÂY
Để tìm hiểu thêm nhiều kiến thức bổ ích và tự mình xây dựng nhiều nhóm hàm công thức phức tạp các bạn hãy tham gia khóa học Đào tạo VBA trong Excel cơ bản và trải nghiệm nhé.