2008年9月2日星期二

关于EXCEL里面的汉字转拼音首字母的函数

关于EXCEL里面的汉字转拼音首字母的函数

近日由于制作通讯录需要,在EXCEL中需要对人名转换为拼音首字母,以前是用了一款“实用汉字转拼音”的免费软件的,后来在excelhome和officefans中看到了好些关于这个的文章,因而本次制作采用了如下的第2种方法。也搜索了一下,故记录之,有些网站地址也没有记全,只能对不起原作者了。

1. 采用定义名称和LOOKUP的方法

定义一个名称叫“拼音”的,做法: 插入->名称->定义 ,输入名称“拼音”,添加,在引用位置中键入
“={"","";"吖","A";"八","B";"嚓","C";"咑","D";"鵽","E";"发","F";"猤","G";"铪","H";" 夻","J";"咔","K";"垃","L";"嘸","M";"旀","N";"噢","O";"妑","P";"七","Q";"囕","R";" 仨","S";"他","T";"屲","W";"夕","X";"丫","Y";"帀","Z"}”
注,不包括外面的引号(下同)。

那么,比如在A3中有"陈",则在B3中输入"=VLOOKUP(A3,拼音,2)",则会得到"C"。

不过此法有时候也会出错,比如"黄","宏",就会得到J,"奕"会得到"Z",看来还不够完善。

2. 自定义函数。
''''''''''''''''''''''
'VBscript代码提供者:小白
'联系方法:
'E-mail:xiaobai@17560.net
'
'VBA代码转换者:惟惟
'联系方法:Email:wuweiyin@yeah.net
'个人博客:http://weiwei.blog.163.com
'如有不正确的地方,请提出来或是帮忙完善
'谢谢
''''''''''''''''''''''''''''''
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
getpychar = "K"
ElseIf (tmp >= 49324 And tmp <= 49895) Then
getpychar = "L"
ElseIf (tmp >= 49896 And tmp <= 50370) Then
getpychar = "M"
ElseIf (tmp >= 50371 And tmp <= 50613) Then
getpychar = "N"
ElseIf (tmp >= 50614 And tmp <= 50621) Then
getpychar = "O"
ElseIf (tmp >= 50622 And tmp <= 50905) Then
getpychar = "P"
ElseIf (tmp >= 50906 And tmp <= 51386) Then
getpychar = "Q"
ElseIf (tmp >= 51387 And tmp <= 51445) Then
getpychar = "R"
ElseIf (tmp >= 51446 And tmp <= 52217) Then
getpychar = "S"
ElseIf (tmp >= 52218 And tmp <= 52697) Then
getpychar = "T"
ElseIf (tmp >= 52698 And tmp <= 52979) Then
getpychar = "W"
ElseIf (tmp >= 52980 And tmp <= 53640) Then
getpychar = "X"
ElseIf (tmp >= 53689 And tmp <= 54480) Then
getpychar = "Y"
ElseIf (tmp >= 54481 And tmp <= 62289) Then
getpychar = "Z"
Else '如果不是中文,则不处理
getpychar = char
End If
End Function

Function getpy(str)
For i = 1 To Len(str)
getpy = getpy & getpychar(Mid(str, i, 1))
Next i
End Function

那么,在VBA编辑器里面->插入->模块->贴入上述代码,那么。
那么,比如在A3中有"实验室",则在B3中输入"=getpy(A3)",则会得到"SYS"。
不过此法有时候也会出错,比如"雯","雪","学"等,就无法得到拼音,看来也还不够完善。

3.利用微软拼音(这个是整词转换的)
本文较长,我也没有仔细用过,不能评价
http://www.excelpx.com/home/show.aspx?id=199&cid=15

http://club.excelhome.net/dispbbs.asp?BoardID=2&ID=229924&replyID=&skin=0
功能比较强大的

4.也有直接建立汉字和拼音的对应库表,直接查询的。:)