EXCEL中使用VBA自定义函数搞定繁体字与简体字互转
我在做VBA服务的时候,常常会遇到香港或台湾的朋友,所以就有简体和繁体转换的需求,有些表格在大陆和港台2地来回转,书写的人一会简体一会繁体就造成了表格简繁不一,后期查找计算的时候会造成诸多不便,这里咱们利用系统API函数,用VBA写2个自定义函数,就可以轻松地解决简繁转换,看到VBA不用害怕,因为你不需要懂,只需要按步骤一步步操作就可以用它
效果示例如下
打开你的EXCEL表,按下ALT+F11,打开EXCEL的vba编辑器,在左侧工程管理器区域右键插入模块
在模块里粘贴文章下面代码,在需要转为简体的地方,用=FtoJ(A1)公式,在需要转为繁体的地方用=JtoF(A1)即可
Option Explicit
'声明部分:
Private Declare 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 Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
'函数部分:
'简转繁
Public Function JtoF(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
JtoF = STf
End Function
'繁转简
Public Function FtoJ(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
FtoJ = STj
End Function