EXCEL技巧——导出汉字姓名首字母

我们在信息表里面要常常用到姓名,那么如何把姓名转化为拼音的首字母呢?这里我们来学习以下方法。

操作方法

  • 01

    导入一张含有姓名的表格,把姓名放到A列中,在B列中将要导出拼音。

  • 02

    现在我们按住Alt+F11,打开VB编辑窗口,单击插入——模块,弹出代码编辑框。

  • 03

    在代码编辑框中输入: Option Explicit Function PY(TT As String) As Variant Dim i%, temp$ PY = "" For i = 1 To Len(TT) temp = Asc(Mid$(TT, i, 1)) If temp > 255 Or temp < 0 Then PY = PY & pinyin(Mid$(TT, i, 1)) Else PY = PY & LCase(Mid$(TT, i, 1)) End If Next i End Function Function pinyin(myStr As String) As Variant On Error Resume Next myStr = StrConv(myStr, vbNarrow) If Asc(myStr) > 0 Or Err.Number = 1004 Then pinyin = "" pinyin = Application.WorksheetFunction.VLookup(myStr, [{"吖","A";"八","B";"嚓","C";"搭","D";"蛾","E";"发","F";"噶","G";"铪","H";"击","J";"咔","K";"垃","L";"妈","M";"拿","N";"噢","O";"啪","P";"七","Q";"然","R";"仨","S";"他","T";"挖","W";"夕","X";"压","Y";"座","Z"}], 2) End Function 关闭VB窗口。

  • 04

    现在转到工作表中,我们在B2中输入=PY(A2),回车我们就看到姓名的拼音首字母显示出来了。

  • 05

    我们用填充手柄把B列中的单元格全部填满,这时候我们要的效果就出现了。

(0)

相关推荐