2021-05-12 14:32:11
EXCEL技巧——匯出漢字姓名首字母
我們在資訊表裡面要常常用到姓名,那麼如何把姓名轉化為拼音的首字母呢?這裡我們來學習以下方法。
1
匯入一張含有姓名的表格,把姓名放到A列中,在B列中將要匯出拼音。
2
現在我們按住Alt+F11,開啟VB編輯視窗,單擊插入——模組,彈出程式碼編輯框。
3
在程式碼編輯框中輸入:
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視窗。
4
現在轉到工作表中,我們在B2中輸入=PY(A2),回車我們就看到姓名的拼音首字母顯示出來了。
5
我們用填充手柄把B列中的單元格全部填滿,這時候我們要的效果就出現了。
相關文章