微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

转换Excel功能或VBA以在Word宏中使用

如何解决转换Excel功能或VBA以在Word宏中使用

我在Excel中使用以下功能或VBA将数字转换为字母。 此代码在Excel中效果很好。

Global AlphaNumeric1(0 To 19) As String
Global AlphaNumeric2(1 To 9) As String
Global AlphaNumeric3(1 To 9) As String 
Sub alphaset()
   Dim i%
   AlphaNumeric1(0) = "Zero"
   AlphaNumeric1(1) = "One"
.
.
.
   AlphaNumeric1(19) = "Nineteen"
      
   AlphaNumeric2(1) = "Ten "
.
.
.
   AlphaNumeric2(9) = "Ninety "
   
   AlphaNumeric3(1) = "One hundred"
.
.
.
   AlphaNumeric3(9) = "nine hundred"       
End Sub
Function Digit2Word(Number As String) As String   
   alphaset
    Dim No As Currency,N As String
    On Error GoTo Digit2Worderror
    No = CCur(Number)
    N = CStr(No)
    Select Case Len(N)
        Case 1 To 3:
                If N < 20 Then
                    Digit2Word= AlphaNumeric1(N)
                ElseIf N < 100 Then
                    If N Mod 10 = 0 Then
                        Digit2Word= AlphaNumeric2(N \ 10)
                    Else
                        Digit2Word= AlphaNumeric2(N \ 10) & " and " & Digit2Word(N Mod 10)
                    End If
                ElseIf N < 1000 Then
                    If N Mod 100 = 0 Then
                        Digit2Word= AlphaNumeric3(N \ 100)
                    Else
                        Digit2Word= AlphaNumeric3(N \ 100) & " and " & Digit2Word(N Mod 100)
                    End If
                End If
        Case 4 To 6:
                If (Right(N,3)) = 0 Then
                   Digit2Word= Digit2Word(Left(N,Len(N) - 3)) & " Thousand  "
                Else
                    Digit2Word= Digit2Word(Left(N,Len(N) - 3)) & " Thousand and " & Digit2Word(Right(N,3))
                End If
        Case 7 To 9:
                If (Right(N,6)) = 0 Then
                   Digit2Word= Digit2Word(Left(N,Len(N) - 6)) & " Million "
                Else
                    Digit2Word= Digit2Word(Left(N,Len(N) - 6)) & " Million and " & Digit2Word(Right(N,6))
                End If
        Case Else:
                If (Right(N,9)) = 0 Then
                   Digit2Word= Digit2Word(Left(N,Len(N) - 9)) & " Billion "
                Else
                    Digit2Word= Digit2Word(Left(N,Len(N) - 9)) & " Billion and " & Digit2Word(Right(N,9))
                End If
    End Select
    Exit Function
Digit2Worderror:
    Digit2Word= "#Error"
End Function

如何通过运行Macro将该VBA转换为在Word中使用? (例如,选择鼠标指针打开的数字并将其转换为字母。在Word中运行宏,而无需链接到Excel)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。