| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1861 人关注过本帖
标题:求助,EXcel中把数字转换为英文想在前面加一些字符该怎么加
只看楼主 加入收藏
lx335482
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-1-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
求助,EXcel中把数字转换为英文想在前面加一些字符该怎么加
Option Explicit
Dim StrNO(19) As String
Dim Unit(8) As String
Dim StrTENs(9) As String
Public Function NumberToString(Number As Double) As String
    Dim Str As String, BeforePoint As String, AfterPoint As String, tmpStr As String
    Dim Point As Integer
    Dim nBit As Integer
    Dim CurString As String
    Dim nNumLen As Integer
    Dim T As String
    Call Init
    Str = CStr(Round(Number, 2))
    ' Str = Number
    If InStr(1, Str, ".") = 0 Then
        BeforePoint = Str
        AfterPoint = ""
    Else
        BeforePoint = Left(Str, InStr(1, Str, ".") - 1)
        T = Right(Str, Len(Str) - InStr(1, Str, "."))
        If Len(T) < 2 Then AfterPoint = Val(T) * 10
        If Len(T) = 2 Then AfterPoint = Val(T)
        If Len(T) > 2 Then AfterPoint = Val(Left(T, 2))
    End If
    If Len(BeforePoint) > 12 Then
        NumberToString = "Too Big."
        Exit Function
    End If
    Str = ""
    Do While Len(BeforePoint) > 0
        nNumLen = Len(BeforePoint)
        If nNumLen Mod 3 = 0 Then
            CurString = Left(BeforePoint, 3)
            BeforePoint = Right(BeforePoint, nNumLen - 3)
        Else
            CurString = Left(BeforePoint, (nNumLen Mod 3))
            BeforePoint = Right(BeforePoint, nNumLen - (nNumLen Mod 3))
        End If
        nBit = Len(BeforePoint) / 3
        tmpStr = DecodeHundred(CurString)
        If (BeforePoint = String(Len(BeforePoint), "0") Or nBit = 0) And Len(CurString) = 3 Then
            If CInt(Left(CurString, 1)) <> 0 And CInt(Right(CurString, 2)) <> 0 Then
                'tmpStr = Left(tmpStr, InStr(1, tmpStr, Unit(4)) + Len(Unit(4))) & Unit(8) & " " & Right(tmpStr, Len(tmpStr) - (InStr(1, tmpStr, Unit(4)) + Len(Unit(4))))
            Else 'If CInt(Left(CurString, 1)) <> 0 And CInt(Right(CurString, 2)) = 0 Then
                'tmpStr = Unit(8) & " " & tmpStr
            End If
        End If
        If nBit = 0 Then
            Str = Trim(Str & " " & tmpStr)
        Else
            Str = Trim(Str & " " & tmpStr & " " & Unit(nBit))
        End If
        If Left(Str, 3) = Unit(8) Then Str = Trim(Right(Str, Len(Str) - 3))
        If BeforePoint = String(Len(BeforePoint), "0") Then Exit Do
        'Debug.Print Str
    Loop
    BeforePoint = Str
    If Len(AfterPoint) > 0 Then
        AfterPoint = Unit(6) & " " & DecodeHundred(AfterPoint) & " " & Unit(5)
    Else
        AfterPoint = Unit(5)
    End If
    NumberToString = BeforePoint & " " & AfterPoint
End Function
Private Function DecodeHundred(HundredString As String) As String
    Dim tmp As Integer
    If Len(HundredString) > 0 And Len(HundredString) <= 3 Then
        Select Case Len(HundredString)
            Case 1
                tmp = CInt(HundredString)
                If tmp <> 0 Then DecodeHundred = StrNO(tmp)
            Case 2
                tmp = CInt(HundredString)
                If tmp <> 0 Then
                    If (tmp < 20) Then
                        DecodeHundred = StrNO(tmp)
                    Else
                        If CInt(Right(HundredString, 1)) = 0 Then
                            DecodeHundred = StrTENs(Int(tmp / 10))
                        Else
                            DecodeHundred = StrTENs(Int(tmp / 10)) & "-" & StrNO(CInt(Right(HundredString, 1)))
                        End If
                    End If
                End If
            Case 3
                If CInt(Left(HundredString, 1)) <> 0 Then
                    DecodeHundred = StrNO(CInt(Left(HundredString, 1))) & " " & Unit(4) & " " & DecodeHundred(Right(HundredString, 2))
                Else
                    DecodeHundred = DecodeHundred(Right(HundredString, 2))
                End If
            Case Else
        End Select
    End If
End Function
Private Sub Init()
    If StrNO(1) <> "ONE" Then
        StrNO(1) = "ONE"
        StrNO(2) = "TWO"
        StrNO(3) = "THREE"
        StrNO(4) = "FOUR"
        StrNO(5) = "FIVE"
        StrNO(6) = "SIX"
        StrNO(7) = "SEVEN"
        StrNO(8) = "EIGHT"
        StrNO(9) = "NINE"
        StrNO(10) = "TEN"
        StrNO(11) = "ELEVEN"
        StrNO(12) = "TWEVEL"
        StrNO(13) = "THIRTEEN"
        StrNO(14) = "FOURTEEN"
        StrNO(15) = "FIFTEEN"
        StrNO(16) = "SIXTEEN"
        StrNO(17) = "SEVENTEEN"
        StrNO(18) = "EIGHTEEN"
        StrNO(19) = "NINETEEN"
        StrTENs(1) = "TEN"
        StrTENs(2) = "TWENTY"
        StrTENs(3) = "THIRTY"
        StrTENs(4) = "FORTY"
        StrTENs(5) = "FIFTY"
        StrTENs(6) = "SIXTY"
        StrTENs(7) = "SEVENTY"
        StrTENs(8) = "EIGHTy"
        StrTENs(9) = "NINETY"
        Unit(1) = "THOUSAND"
        Unit(2) = "Million"
        Unit(3) = "Billion"
        Unit(4) = "HUNDRED"
        Unit(5) = "ONLY"
        Unit(6) = "CENTS"
        Unit(7) = "Cents"
        Unit(8) = "And"
    End If
End Sub
小白表示加不进去,我想在输出结果的前面加上SAY US DOLLARS,来个大侠帮帮我可好
搜索更多相关主题的帖子: 英文 
2017-01-11 20:08
lx335482
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-1-11
收藏
得分:0 
没人鸟我吗
2017-01-11 20:10
lx335482
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-1-11
收藏
得分:0 
大神在哪里
2017-01-11 20:46
金戈铁马2
Rank: 2
等 级:论坛游民
帖 子:83
专家分:85
注 册:2017-1-8
收藏
得分:20 
谢谢!
2017-01-12 12:34
快速回复:求助,EXcel中把数字转换为英文想在前面加一些字符该怎么加
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017075 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved