Excel中把汉字转换成拼音码

1.启动Excel 2003(其它版本请仿照操作),打开相应的工作表;

2.执行“工具→宏→Visual Basic编辑器”命令(或者直接按“Alt+F11”组合键),进入Visual Basic编辑状态;

3.执行“插入→模块”命令,插入一个新模块。再双击插入的模块,进入模块代码编辑状态;

4.将代码输入其中;

 1 Function pinyin(p As String) As String
 2 i = Asc(p)
 3 Select Case i
 4 Case -20319 To -20284: pinyin = "A"
 5 Case -20283 To -19776: pinyin = "B"
 6 Case -19775 To -19219: pinyin = "C"
 7 Case -19218 To -18711: pinyin = "D"
 8 Case -18710 To -18527: pinyin = "E"
 9 Case -18526 To -18240: pinyin = "F"
10 Case -18239 To -17923: pinyin = "G"
11 Case -17922 To -17418: pinyin = "H"
12 Case -17417 To -16475: pinyin = "J"
13 Case -16474 To -16213: pinyin = "K"
14 Case -16212 To -15641: pinyin = "L"
15 Case -15640 To -15166: pinyin = "M"
16 Case -15165 To -14923: pinyin = "N"
17 Case -14922 To -14915: pinyin = "O"
18 Case -14914 To -14631: pinyin = "P"
19 Case -14630 To -14150: pinyin = "Q"
20 Case -14149 To -14091: pinyin = "R"
21 Case -14090 To -13319: pinyin = "S"
22 Case -13318 To -12839: pinyin = "T"
23 Case -12838 To -12557: pinyin = "W"
24 Case -12556 To -11848: pinyin = "X"
25 Case -11847 To -11056: pinyin = "Y"
26 Case -11055 To -2050: pinyin = "Z"
27 Case Else: pinyin = p
28 End Select
29 End Function
30 Function getpy(str)
31 For i = 1 To Len(str)
32 getpy = getpy & pinyin(Mid(str, i, 1))
33 Next i
34 End Function

5.代码输入完成后,关闭Visual Basic编辑窗口,返回Excel编辑状态;

6.自定义函数就可以用了,如:输入公式:=getpy(A2)

7.生成后是大写,如需大小写转换可用以下函数:
转换为小写:=LOWER(字符串)
转换为大写:=UPPER(字符串)
首字母大写:=PROPER(字符串)

转自:信息来源

文末自警:1.本函数会用但是并未真正搞懂,跟那篇【SQL中把汉字转换拼音码】一样,仍然需要进一步学习深究函数算法原理。

              2.VB中有for循环,SQL中没有for循环,【SQL中把汉字转换拼音码】中,循环用的是While循环,这点区别要注意,学会就地取材,深

                入所使用的语言去编写程序。

posted @ 2022-05-28 12:54  美人她爹  阅读(1125)  评论(0编辑  收藏  举报