材料/工具
excel
方法
在電腦中打開要編輯的excel文檔
按下Alt+F11鍵,打開VBE界面,如下圖所示。在VBE界面中,單擊圖片中所示的左上角空白處,選擇“插入“,”模塊“
此時(shí),就插入了一個(gè)模塊,后面的代碼就要放到這個(gè)模塊中,保證右側(cè)空白區(qū)域是模塊代碼的編輯區(qū)(最保險(xiǎn)的做法是雙擊模塊),然后執(zhí)行下一步操作。
將下面的代碼全部復(fù)制粘貼到空白區(qū)域。
Function pinyin(p As String) As String
i = Asc(p)
Select Case i
Case -20319 To -20284: pinyin = "A"
Case -20283 To -19776: pinyin = "B"
Case -19775 To -19219: pinyin = "C"
Case -19218 To -18711: pinyin = "D"
Case -18710 To -18527: pinyin = "E"
Case -18526 To -18240: pinyin = "F"
Case -18239 To -17923: pinyin = "G"
Case -17922 To -17418: pinyin = "H"
Case -17417 To -16475: pinyin = "J"
Case -16474 To -16213: pinyin = "K"
Case -16212 To -15641: pinyin = "L"
Case -15640 To -15166: pinyin = "M"
Case -15165 To -14923: pinyin = "N"
Case -14922 To -14915: pinyin = "O"
Case -14914 To -14631: pinyin = "P"
Case -14630 To -14150: pinyin = "Q"
Case -14149 To -14091: pinyin = "R"
Case -14090 To -13319: pinyin = "S"
Case -13318 To -12839: pinyin = "T"
Case -12838 To -12557: pinyin = "W"
Case -12556 To -11848: pinyin = "X"
Case -11847 To -11056: pinyin = "Y"
Case -11055 To -2050: pinyin = "Z"
Case Else: pinyin = p
End Select
End Function
Function getpy(str)
For i = 1 To Len(str)
getpy = getpy & pinyin(Mid(str, i, 1))
Next i
End Function
然后關(guān)閉這個(gè)新界面,返回Excel,就可以像Excel自帶的函數(shù)一樣使用這個(gè)自定義的Getpy函數(shù)了,它只有一個(gè)參數(shù),參數(shù)可以直接指定單元格,也可以直接輸入字符。
需要注意的是,Getpy函數(shù),只返回漢字拼音的首字母,如果是其他的字母或者數(shù)據(jù)等非漢字文本,會(huì)返回原來的值,如圖所示
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com