首页 > 代码库 > 使用VBA函数在Excel单元格中生成拼音缩写

使用VBA函数在Excel单元格中生成拼音缩写

代码库 ,

Function PinYin(Hz As String)
  Dim PinMa As String
  Dim MyPinMa As Variant
  Dim Temp As Integer, i As Integer, j As Integer
  PinMa = "a,20319,"
  PinMa = PinMa & "b,20283,"
  PinMa = PinMa & "c,19775,"
  PinMa = PinMa & "d,19218,"
  PinMa = PinMa & "e,18710,"
  PinMa = PinMa & "f,18526,"
  PinMa = PinMa & "g,18239,"
  PinMa = PinMa & "h,17922,"
  PinMa = PinMa & "j,17417,"
  PinMa = PinMa & "k,16474,"
  PinMa = PinMa & "l,16212,"
  PinMa = PinMa & "m,15640,"
  PinMa = PinMa & "n,15165,"
  PinMa = PinMa & "o,14922,"
  PinMa = PinMa & "p,14914,"
  PinMa = PinMa & "q,14630,"
  PinMa = PinMa & "r,14149,"
  PinMa = PinMa & "s,14090,"
  PinMa = PinMa & "t,13318,"
  PinMa = PinMa & "w,12838,"
  PinMa = PinMa & "x,12556,"
  PinMa = PinMa & "y,11847,"
  PinMa = PinMa & "z,11055,"
  MyPinMa = Split(PinMa, ",")

  For i = 1 To Len(Hz)
  Temp = Asc(Mid(Hz, i, 1))
      If Temp < 0 Then
        Temp = Abs(Temp)
        For j = 45 To 1 Step -2
            If Temp <= Val(MyPinMa(j)) Then
                PinYin = PinYin & MyPinMa(j - 1)
                Exit For
            End If
        Next
      Else
         ' 保留非汉字字符
      PinYin = PinYin & Mid(Hz, i, 1)
      End If
  Next
  PinYin = Trim(PinYin)
End Function

本博客遵循CC协议2.5,即署名-非商业性使用-相同方式共享
写作很辛苦,转载请注明作者以及原文链接~
如果你喜欢我的文章,你可以订阅我的博客:-D点击订阅我的文章

  1. X﹏X 到现在还没有评论~
  1. 暂时没有trackbacks.