excel:汉字拼音首字母提取

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

方法一:VBA自定义函数"=hztopy()"

下面是一个VBA自定义函数,工具->宏->Visual Basic编辑器->,插入模块,在VBA编辑器里面->插入->模块->贴入下述代码,(按ALT+F11,插入模块,在右面窗口中粘贴下面代码)那么,比如在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 = pystring

End 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 <= 45252) Then

getpychar = "A"

ElseIf (tmp >= 45253 And tmp <= 45760) Then

getpychar = "B"

ElseIf (tmp >= 45761 And tmp <= 46317) Then

getpychar = "C"

ElseIf (tmp >= 46318 And tmp <= 46825) Then

getpychar = "D"

ElseIf (tmp >= 46826 And tmp <= 47009) Then

getpychar = "E"

ElseIf (tmp >= 47010 And tmp <= 47296) Then

getpychar = "F"

ElseIf (tmp >= 47297 And tmp <= 47613) Then

getpychar = "G"

ElseIf (tmp >= 47614 And tmp <= 48118) Then

getpychar = "H"

ElseIf (tmp >= 48119 And tmp <= 49061) Then

getpychar = "J"

ElseIf (tmp >= 49062 And tmp <= 49323) Then

相关文档
最新文档