topcss / my-notes

https://github.com/topcss/my-notes/issues/new

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Excel 调用DLL实现繁简体相互转换

topcss opened this issue · comments

commented

Excel 调用DLL实现繁简体相互转换

一、调试篇

  1. 在 Excel 中,按 Alt+F11 打开VBA编程环境。点击菜单中“插入”-“模块” 打开编辑框,或按下 ALT+I+M,粘贴下面的代码。
Private Declare PtrSafe Function LCMapString Lib "kernel32" Alias "LCMapStringA" (ByVal Locale As Long, ByVal dwMapFlags As Long, ByVal lpSrcStr As String, ByVal cchSrc As Long, ByVal lpDestStr As String, ByVal cchDest As Long) As Long
Private Declare PtrSafe Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long

Public Function 简转繁(ByVal Str As String) As String
    Dim STlen As Long
    Dim STf As String
    STlen = lstrlen(Str)
    STf = Space(STlen)
    LCMapString &H804, &H4000000, Str, STlen, STf, STlen
    简转繁 = STf
End Function

Public Function 繁转简(ByVal Str As String) As String
    Dim STlen As Long
    Dim STj As String
    STlen = lstrlen(Str)
    STj = Space(STlen)
    LCMapString &H804, &H2000000, Str, STlen, STj, STlen
    繁转简 = STj
End Function

Sub test()
    Debug.Print 简转繁("东西"), 繁转简("東西")
End Sub
  1. 选择菜单”视图“-”立即窗口“或按 Ctrl+G 打开立即窗口,用于监控打印结果。
  2. 按 F5 弹出”宏“窗口,选择”test“点击”运行“,即可查看运行结果。如图1所示。

image
图1

二、应用篇

  1. 关闭 VBA 窗口,回到 Excel 主界面
  2. 例如:我们想把 B2 单元格中的简体转换为繁体,在 B3 单元格中显示
  3. B3 单元格 中输入 =简转繁(B2) 即可,实现转换功能