How to convert Chinese characters in cells into initial letters using Excel

In a recent project, the client confirmed that the data was processed through Excel tables. For example, column A is the text, and column B is the first letter of the text in column A. This requirement can also be handled by writing SQL, but it is faster to import the data through Excel. The following is the processing method.
1. Start Excel and press the Alt+F11 keys at the same time to open the VBE interface, as shown in the figure below. In the VBE interface, click on the blank space in the upper left corner as shown in the picture and select "Insert", "Module".
This picture comes from the Internet, borrow it, if there is any infringement, please contact us and delete it.
2. At this time, a module is inserted, and the following code must be placed in this module. Make sure that the blank area on the right is the editing area of ​​the module code (the safest way is to double-click the module), and then perform the next step.
This picture comes from the Internet, borrow it, if there is any infringement, please contact us and delete it.
3. Copy and paste all the following code into the blank area.

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

Then ctrl+s to save, he will prompt to save, select yes all the way.

4. Return to the Excel table. For example, the table we need to process is column A, and the table to be generated is column B, then write the following formula

=getpy(A2)

The above means, select cell B2. Then enter the formula =getpy(A2), b2 will get the first letter of the Chinese character of A2, if you want more than one. Just ctrl plus the small + sign under the table and keep pushing and it's done.

Guess you like

Origin blog.csdn.net/likeni1314/article/details/108634075