资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
方法一:VBA自定义函数=hztopy()下面是一个VBA自定义函数,工具-宏-Visual Basic编辑器-,插入模块,在VBA编辑器里面-插入-模块-贴入下述代码,(按ALTF11,插入模块,在右面窗口中粘贴下面代码)那么,比如在A3中有进退两难,则在B3中输入=hztopy(A3),则会得到JTLN。不过此法有时候也会出错,比如皓、鑫、婷、雯、奕等,就无法提取汉字拼音首字母。Function hztopy(hzpy As String) As String Dim hzstring As String, pystring As String Dim hzpysum As Integer, hzi As Integer, hzpyhex As Integer hzstring = Trim(hzpy) hzpysum = Len(Trim(hzstring) pystring = For hzi = 1 To hzpysum hzpyhex = &H + Hex(Asc(Mid(hzstring, hzi, 1) Select Case hzpyhex Case &HB0A1 To &HB0C4: pystring = pystring + A Case &HB0C5 To &HB2C0: pystring = pystring + B Case &HB2C1 To &HB4ED: pystring = pystring + C Case &HB4EE To &HB6E9: pystring = pystring + D Case &HB6EA To &HB7A1: pystring = pystring + E Case &HB7A2 To &HB8C0: pystring = pystring + F Case &HB8C1 To &HB9FD: pystring = pystring + G Case &HB9FE To &HBBF6: pystring = pystring + H Case &HBBF7 To &HBFA5: pystring = pystring + J Case &HBFA6 To &HC0AB: pystring = pystring + K Case &HC0AC To &HC2E7: pystring = pystring + L Case &HC2E8 To &HC4C2: pystring = pystring + M Case &HC4C3 To &HC5B5: pystring = pystring + N Case &HC5B6 To &HC5BD: pystring = pystring + O Case &HC5BE To &HC6D9: pystring = pystring + P Case &HC6DA To &HC8BA: pystring = pystring + Q Case &HC8BB To &HC8F5: pystring = pystring + R Case &HC8F6 To &HCBF9: pystring = pystring + S Case &HCBFA To &HCDD9: pystring = pystring + T Case &HEDC5: pystring = pystring + T Case &HCDDA To &HCEF3: pystring = pystring + W Case &HCEF4 To &HD1B8: pystring = pystring + X Case &HD1B9 To &HD4D0: pystring = pystring + Y Case &HD4D1 To &HD7F9: pystring = pystring + Z Case Else pystring = pystring + Mid(hzstring, hzi, 1) End Select Next hztopy = pystringEnd Function方法二:VBA自定义函数=getpy() 下面是一个VBA自定义函数,工具-宏-Visual Basic编辑器-,插入模块,在VBA编辑器里面-插入-模块-贴入上述代码,那么。那么,比如在A2中有进退两难,则在B2中输入=getpy(A2),则会得到JTLN。不过此法有时候也会出错,比如鑫、雯、奕等,就无法提取汉字拼音首字母,还有些字如皓、婷等被错提取为“Z”。Function getpychar(char)tmp = 65536 + Asc(char)If (tmp = 45217 And tmp = 45253 And tmp = 45761 And tmp = 46318 And tmp = 46826 And tmp = 47010 And tmp = 47297 And tmp = 47614 And tmp = 48119 And tmp = 49062 And tmp = 49324 And tmp = 49896 And tmp = 50371 And tmp = 50614 And tmp = 50622 And tmp = 50906 And tmp = 51387 And tmp = 51446 And tmp = 52218 And tmp = 52698 And tmp = 52980 And tmp = 53689 And tmp = 54481 And tmp = 62289) Thengetpychar = ZElse 如果不是中文,则不处理getpychar = charEnd IfEnd FunctionFunction getpy(str)For i = 1 To Len(str)getpy = getpy & getpychar(Mid(str, i, 1)Next iEnd Function方法三:Excel函数LOOKUP这个函数在Excel里面最多只能设置自动提取字段的前四个字的拼音首字母,多了会提示公式太长。比如在A2中有进退两难,则在B2中输入如下函数=LOOKUP(),则会得到JTLN。提取前四个字首字母时的函数=LOOKUP(CODE(A2),45217+0,36,544,1101,1609,1793,2080,2560,2902,3845,4107,4679,5154,5397,5405,5689,6170,6229,7001,7481,7763,8472,9264,A,B,C,D,E,F,G,H,J,K,L,M,N,O,P,Q,R,S,T,W,X,Y,Z)&LOOKUP(CODE(MID(A2,2,1),45217+0,36,544,1101,1609,1793,2080,2560,2902,3845,4107,4679,5154,5397,5405,5689,6170,6229,7001,7481,7763,8472,9264,A,B,C,D,E,F,G,H,J,K,L,M,N,O,P,Q,R,S,T,W,X,Y,Z)&LOOKUP(CODE(MID(A2,3,1),45217+0,36,544,1101,1609,1793,2080,2560,2902,3845,4107,4679,5154,5397,5405,5689,6170,6229,7001,7481,7763,8472,9264,A,B,C,D,E,F,G,H,J,K,L,M,N,O,P,Q,R,S,T,W,X,Y,Z)&LOOKUP(CODE(MID(A2,4,1),45217+0,36,544,1101,1609,1793,2080,2560,2902,3845,4107,4679,5154,5397,5405,5689,6170,6229,7001,7481,7763,8472,9264,A,B,C,D,E,F,G,H,J,K,L,M,N,O,P,Q,R,S,T,W,X,Y,Z)提取前二个字首字母时的函数=LOOKUP(CODE(A2),4521
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号