excel中如何实现提取汉字的拼音首字母?

汉字中还包含英文字母或数字,英文或数字部分也要返回。
比如:中国航天6号a
返回应为:ZGHT6Ha
非常好,谢谢了
再请教一个问题:如果我想改ZGHT6Ha中一个字母,不想全部重新输入行不?
我现在一点就出公式,没办法只改一个字母

还一个问题就是:其中的符号不想要了,比如说“sa-s”,只要“sas”就行了
最新回答
旧事酒浓

2024-05-31 10:21:31

后面又提的问题,手工做只需选中ZGHT6Ha这一列复制,再另一列选择性粘贴->数值,再改就行了。

下面是一个VBA自定义函数,按ALT+F11,插入模块,在右面窗口中粘贴下面代码。
若B10内容为 中国航天6号a,则在某空白格中输入:=hztopy(B10) 即可

Function hztopy(hzpy As String) As String
Dim hzstring As String, pystring As String
Dim hzpysum As Integer, hzi As Integer, hzpyhex As Integer
hzstring = Trim(hzpy)
hzpysum = Len(Trim(hzstring))
pystring = ""
For hzi = 1 To hzpysum
hzpyhex = "&H" + Hex(Asc(Mid(hzstring, hzi, 1)))
Select Case hzpyhex
Case &HB0A1 To &HB0C4: pystring = pystring + "A"
Case &HB0C5 To &HB2C0: pystring = pystring + "B"
Case &HB2C1 To &HB4ED: pystring = pystring + "C"
Case &HB4EE To &HB6E9: pystring = pystring + "D"
Case &HB6EA To &HB7A1: pystring = pystring + "E"
Case &HB7A2 To &HB8C0: pystring = pystring + "F"
Case &HB8C1 To &HB9FD: pystring = pystring + "G"
Case &HB9FE To &HBBF6: pystring = pystring + "H"
Case &HBBF7 To &HBFA5: pystring = pystring + "J"
Case &HBFA6 To &HC0AB: pystring = pystring + "K"
Case &HC0AC To &HC2E7: pystring = pystring + "L"
Case &HC2E8 To &HC4C2: pystring = pystring + "M"
Case &HC4C3 To &HC5B5: pystring = pystring + "N"
Case &HC5B6 To &HC5BD: pystring = pystring + "O"
Case &HC5BE To &HC6D9: pystring = pystring + "P"
Case &HC6DA To &HC8BA: pystring = pystring + "Q"
Case &HC8BB To &HC8F5: pystring = pystring + "R"
Case &HC8F6 To &HCBF9: pystring = pystring + "S"
Case &HCBFA To &HCDD9: pystring = pystring + "T"
Case &HEDC5: pystring = pystring + "T"
Case &HCDDA To &HCEF3: pystring = pystring + "W"
Case &HCEF4 To &HD1B8: pystring = pystring + "X"
Case &HD1B9 To &HD4D0: pystring = pystring + "Y"
Case &HD4D1 To &HD7F9: pystring = pystring + "Z"
Case Else
pystring = pystring + Mid(hzstring, hzi, 1)
End Select
Next
hztopy = pystring
End Function
风铃鹿

2024-05-31 10:08:13

步骤:
第一步、启动Excel
2003(其它版本请仿照操作),打开相应的工作表;
第二步、执行“工具→宏→Visual
Basic编辑器”命令(或者直接按“Alt+F11”组合键),进入Visual
Basic编辑状态;
第三步、执行“插入→模块”命令,插入一个新模块。再双击插入的模块,进入模块代码编辑状态;
第四步、将代码输入其中;
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
第五步、代码输入完成后,关闭Visual
Basic编辑窗口,返回Excel编辑状态;
第六步、自定义函数就可以用了,如:选中A2单元格,输入公式:=getpy(A2)。