excel 获取中文拼音首字母
CreateTime--2018年5月31日08:50:42
Author:Marydon
1.情景展示
想要获取姓名的拼音首字母
2.实现方式
通过使用excel的宏命令来实现
第一步:开启宏命令;
第二步:创建宏命令
切换到宏视图
插入-->模块
将下面代码粘贴到弹出的模块命令窗口
Function pinyin(p As String) As String
i = Asc(p)
Select Case i
Case - To -: pinyin = "A"
Case - To -: pinyin = "B"
Case - To -: pinyin = "C"
Case - To -: pinyin = "D"
Case - To -: pinyin = "E"
Case - To -: pinyin = "F"
Case - To -: pinyin = "G"
Case - To -: pinyin = "H"
Case - To -: pinyin = "J"
Case - To -: pinyin = "K"
Case - To -: pinyin = "L"
Case - To -: pinyin = "M"
Case - To -: pinyin = "N"
Case - To -: pinyin = "O"
Case - To -: pinyin = "P"
Case - To -: pinyin = "Q"
Case - To -: pinyin = "R"
Case - To -: pinyin = "S"
Case - To -: pinyin = "T"
Case - To -: pinyin = "W"
Case - To -: pinyin = "X"
Case - To -: pinyin = "Y"
Case - To -: pinyin = "Z"
Case Else: pinyin = p
End Select
End Function
Function getFirstLetter(str)
For i = To Len(str)
getFirstLetter = getFirstLetter & pinyin(Mid(str, i, ))
Next i
End Function
如图所示
切换到excel视图
输入=get,刚才创建的命令函数将会出现在候选内容里
输入=getFirstLetter(a2),按回车键即可。
第三步:保存
按Ctrl+s-->
方式一
选择是
方式二
选择否,选择文件类型为"Excel 启用宏的工作薄(*.xlsm)"或"Excel 启用宏的模板(*.xltm)"-->保存
效果展示
区别:
方式一,仅限本次编辑,宏命令有效。
方式二,在该文件中,宏命令始终有效。