EXCEL汉字转拼音 WORD转换法及用VBA宏自建函数
将中文姓名直接转为中文拼音,这里有两个方法提供,一个是用WORD来做中转转换,一个是用VBA宏来转换
一、WORD法
二、VBA宏
Function PinYin(TXT As Variant, Delimiter As String, Tpy As Byte, Optional FirU As Boolean = False) As String 'Delimiter,隔离符号; 'Tpy定义返回格式: '1,带用数字表示的声调 '2,无声调 '3,仅首字母 '4,带传统声调 'FirU,各单词首字母是否大写,默认小写 Dim N As Integer Dim ASCID As Long Dim Y As Byte Dim M_Txt As String Dim M_PY As String Dim MI_PY As String On Error Resume Next TXT = Trim(TXT) If TXT = "" Then PinYin = "" Exit Function End If If PY_DB(72, 94) <> "ā/á/ǎ/à" And Tpy = 4 Then Call DealVal_2 ElseIf PY_DB(72, 94) <> "a1" And Tpy < 4 Then Call DealVal_1 End If For i = 1 To Len(Trim(TXT)) M_Txt = Mid(Trim(TXT), i, 1) If M_Txt = "" Then MI_PY = "" Else ASCID = Asc(M_Txt) For N = 1 To UBound(PY_Index) If PY_Index(N) < ASCID Then Exit For Next N PYDB_Index = PY_Index(N - 1) - ASCID If PYDB_Index < 0 Or PYDB_Index > 93 Then M_PY = M_Txt Y = 1 Else M_PY = PY_DB(N - 1, PYDB_Index + 1) End If End If Select Case Tpy Case 1 MI_PY = M_PY Case 2 MI_PY = IIf(M_PY = M_Txt, M_PY, Mid(M_PY, 1, Len(M_PY) - 1)) Case 3 MI_PY = Left(M_PY, 1) Case 4 MI_PY = M_PY End Select PinYin = PinYin & IIf(M_PY = M_Txt, MI_PY, IIf(Y = 1, Delimiter & MI_PY & Delimiter, IIf(i = Len(Trim(TXT)), MI_PY, MI_PY & Delimiter))) Y = IIf(Y = 1, IIf(M_PY = M_Txt, 1, 0), 0) Next i If FirU Then PinYin = Application.WorksheetFunction.Proper(PinYin) End Function
函数的声明:Function PinYin(TXT As Variant, Delimiter As String, Tpy As Byte) As String,
这里第三个参数 Tpy是定义返回格式,主要有以下几种:
1,带用数字表示的声调
2,无声调
3,首字母
4,首字母并大写
5,带传统声调
假设 TXT="my name is: 中国 1."
用 PinYin(TXT," ",5)调用,将得到:my name is: zhōng/zhòng guó 1.
用 PinYin(TXT," ",4,1)调用,将得到:My Name Is: Zhōng/Zhòng Guó 1.
顶(3)
踩(1)
- 最新评论