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

1.office2007导入一张含有姓名的表格,一列中文姓名 一列首字母

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

3.在代码编辑框中复制以下代码,然后关闭VB窗口。

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

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

详细步骤:https://jingyan.baidu.com/article/19192ad831396ae53f57075d.html?qq-pf-to=pcqq.c2c



你可能感兴趣的:(Excel小技巧)