1. 程式人生 > 其它 >Excel中把漢字轉換成拼音碼

Excel中把漢字轉換成拼音碼

1.啟動Excel 2003(其它版本請仿照操作),開啟相應的工作表;

2.執行“工具→巨集→Visual Basic編輯器”命令(或者直接按“Alt+F11”組合鍵),進入Visual Basic編輯狀態;

3.執行“插入→模組”命令,插入一個新模組。再雙擊插入的模組,進入模組程式碼編輯狀態;

4.將程式碼輸入其中;

 1 Function pinyin(p As String) As String
 2 i = Asc(p)
 3 Select Case i
 4 Case -20319 To -20284: pinyin = "A"
 5 Case -20283 To -19776: pinyin = "
B" 6 Case -19775 To -19219: pinyin = "C" 7 Case -19218 To -18711: pinyin = "D" 8 Case -18710 To -18527: pinyin = "E" 9 Case -18526 To -18240: pinyin = "F" 10 Case -18239 To -17923: pinyin = "G" 11 Case -17922 To -17418: pinyin = "H" 12 Case -17417 To -16475: pinyin = "J" 13 Case -16474 To -16213: pinyin = "
K" 14 Case -16212 To -15641: pinyin = "L" 15 Case -15640 To -15166: pinyin = "M" 16 Case -15165 To -14923: pinyin = "N" 17 Case -14922 To -14915: pinyin = "O" 18 Case -14914 To -14631: pinyin = "P" 19 Case -14630 To -14150: pinyin = "Q" 20 Case -14149 To -14091: pinyin = "R" 21 Case -14090 To -13319: pinyin = "
S" 22 Case -13318 To -12839: pinyin = "T" 23 Case -12838 To -12557: pinyin = "W" 24 Case -12556 To -11848: pinyin = "X" 25 Case -11847 To -11056: pinyin = "Y" 26 Case -11055 To -2050: pinyin = "Z" 27 Case Else: pinyin = p 28 End Select 29 End Function 30 Function getpy(str) 31 For i = 1 To Len(str) 32 getpy = getpy & pinyin(Mid(str, i, 1)) 33 Next i 34 End Function

5.程式碼輸入完成後,關閉Visual Basic編輯視窗,返回Excel編輯狀態;

6.自定義函式就可以用了,如:輸入公式:=getpy(A2)

7.生成後是大寫,如需大小寫轉換可用以下函式:
轉換為小寫:=LOWER(字串)
轉換為大寫:=UPPER(字串)
首字母大寫:=PROPER(字串)

轉自:資訊來源