VBS 数字转英文代码

   2015-10-29 0
核心提示:Excel 中将数值转换为英文单词
如何创建名为 SpellNumber 的示例函数
启动 Microsoft Excel。
按 Alt+F11 启动 Visual Basic 编辑器。
在“插入”菜单上,单击“模块”。
在模块表中键入下面的代码。
复制代码 代码如下:

Option Explicit
'Main Function
Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
"00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
Dollars = "No Dollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select
Select Case Cents
Case ""
Cents = " and No Cents"
Case "One"
Cents = " and One Cent"
Case Else
Cents = " and " & Cents & " Cents"
End Select
SpellNumber = Dollars & Cents
End Function

' Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function

' Converts a number from 10 to 99 into text.
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
Case 10: Result = "Ten"
Case 11: Result = "Eleven"
Case 12: Result = "Twelve"
Case 13: Result = "Thirteen"
Case 14: Result = "Fourteen"
Case 15: Result = "Fifteen"
Case 16: Result = "Sixteen"
Case 17: Result = "Seventeen"
Case 18: Result = "Eighteen"
Case 19: Result = "Nineteen"
Case Else
End Select
Else ' If value between 20-99...
Select Case Val(Left(TensText, 1))
Case 2: Result = "Twenty "
Case 3: Result = "Thirty "
Case 4: Result = "Forty "
Case 5: Result = "Fifty "
Case 6: Result = "Sixty "
Case 7: Result = "Seventy "
Case 8: Result = "Eighty "
Case 9: Result = "Ninety "
Case Else
End Select
Result = Result & GetDigit _
(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function

' Converts a number from 1 to 9 into text.
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "One"
Case 2: GetDigit = "Two"
Case 3: GetDigit = "Three"
Case 4: GetDigit = "Four"
Case 5: GetDigit = "Five"
Case 6: GetDigit = "Six"
Case 7: GetDigit = "Seven"
Case 8: GetDigit = "Eight"
Case 9: GetDigit = "Nine"
Case Else: GetDigit = ""
End Select
End Function

如何使用 SpellNumber 示例函数
要使用该示例函数将某数字更改为书面文本,请使用下列示例中演示的方法之一:
回到顶端
方法 1:直接输入
通过将下面的公式输入单元格中,可以将 32.50 更改为“Thirty Two Dollars and Fifty Cents”:
=SpellNumber(32.50)
回到顶端
方法 2:单元格引用
可以引用工作簿中的其他单元格。例如,在单元格 A1 中输入数字 32.50,然后在另一单元格中键入下面的公式:
=SpellNumber(A1)
回到顶端
方法 3:粘贴函数或插入函数
可以使用“粘贴函数”(在 Excel 2000 和 Excel 2002 中)或“插入函数”(在 Excel 2003 中)来将自定义函数输入工作表中。
 
标签: VBS 数字转英文
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • VB用API实现各种对话框(总结) vbs简单代码对话
    VB用API实现各种对话框(总结)  各种对话框(总结)标准对话框(SmDialog)Option Explicit''定义一个全局变量,用于保存字体的各种属性Public Type SmFontAttrFontName As String '字体名FontSize As Integer '字体大小FontBod As Boolean '是否黑体FontItalic A
    02-09
  • 脚本语言语言脚本语言:Shell , JavaScript、VBS
    今天一直在研究脚本语言语言之类的问题,上午正好有机会和大家共享一下.     脚本语言:Shell , JavaScript、VBScript、Perl、PHP、Python、Ruby、Lua                  工作控制语言和shell    Shell 脚本    此类脚本用于自动
    02-09
  • VB中的Dictionary对象 vbs dictionary
     VB中的Dictionary对象     Dictionary对象不是VBA或Visual Basic实时语言的具体存在的部分,它是存在于Microsoft Scripting Runtime Library(SCRRUN.DLL)中的一个对象。    为了在应用程序中使用Dictionary对象,就必须利用Reference对话框增加一个
    02-09
  • VB6文本框中日文乱码的解决办法 vbs写入中文后乱码
    VB6文本框中日文乱码的解决办法 vbs写入中文后
    TextBox控件的Font设置为MS Goshic 太字 并且下面的文字Set设置为 日本语。 
    02-09
  • vb常用函数一览表 vbs函数大全
    常用内部函数数学函数函数功能示例结果说明Abs(x)绝对值Abs(-50.3)50.3 Exp(x)自然指数Exp(2)e^2e(自然对数的底)的某次方Fix(x)取整(取参数的整数部分)Fix(-99.8)-99参数大于 0 时,去掉小数部分;参数小于 0 时,返回大于或等于该参数值Int(x)取整(取小于
    02-08
  • VBA/VB6/VBS/VB.NET/C#/Python/PowerShell都能调用的API封装库
    VBA/VB6/VBS/VB.NET/C#/Python/PowerShell都能
     API函数很强大,但是声明的时候比较繁琐。我开发的封装库,包括窗口、键盘、鼠标、消息等常用功能。用户不需要添加API函数的声明,就可以用到API的功能。在VBA、VB6的引用对话框中引用API.tlb文件,就可以用到其中的函数和方法,在VB.Net或C#项目中引用API.
    02-08
  • 如何在VB.net中建立word文档 vbs创建文本文档
    Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.ClickEnd Subwith:Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.ClickDim oWord As
    02-08
  • VB中的+与&符号的区别 vb和vbs
    字符串运算符和字符串表达式  字符串运算符的作用是将两个字符串连接成一个字符串,经常形象的叫做连接符。  在VB中就提供了两种连接用的字符串运算符 “”、“+” 例如  “VB”+“编程入门” 就等同于“VB编程入门” “VB”“编程入门” 就等同于“V
    02-07
  • 使用 Iisext.vbs 列出Web服务扩展的方法
    这篇文章主要介绍了如何使用 iisext.vbs 在本地或远程计算机上列出 Web 服务扩展,需要的朋友可以参考下
  • 使用 Iisext.vbs 启用Web服务扩展文件的方法
    这篇文章主要介绍了如何使用 iisext.vbs 在本地或远程计算机上启用 Web 服务扩展文件,需要的朋友可以参考下
点击排行