第8章 操作数字

加、减、乘、除、平方与指数(^2 或者^n)、平方根Sqr、正弦Sin、余弦Cos、正切Tan、反正切Atn、绝对值Abs

转换为整型数、长整型数、双精度型数和值

Cint当双精度型数向整型数转换时,CInt通过园整数字得到一个整型数

CLng与CInt相比:当所提供的值较大时使用CLng

Fix函数只是简单地甩掉数字的小数部分,它不做任何园整。Fix能够操作整型数和长整型数

CDbl函数可将提供的参数转换为一个双精度型数

Val函数能给出参数中的数字值。Val返回所有的数字字符直到它遇到一个非数字字符为止

IsNumeric返回一个布尔值(True 或 False) 它考察其参数并确定参数是否为数字

Round函数能够让我们指定小数点后保留的位数

Mod求余 5 Mod 2 = 1

Sgn求数的正负号。如果所给值是负数则返回-1,零返回0,正数返回1

Rnd与Randomize生成随机数

Double 的类型声明字符是数字符号 (#)。

  1. Sub TestRnd()
  2.  
  3. Dim I As Long
  4.  
  5. Dim Lower As Long
  6.  
  7. Dim Higher As Long
  8.  
  9. Dim PointCen( To ) As Point3d
  10.  
  11. Dim PointElem As PointStringElement
  12.  
  13. Dim Num As Double
  14.  
  15. Lower =
  16.  
  17. Higher =
  18.  
  19. Num =
  20.  
  21. Randomize
  22.  
  23. For I = To
  24.  
  25. PointCen().X = Round((Higher - Lower + ) * Rnd(), )
  26.  
  27. PointCen().Y = Round((Higher - Lower + ) * Rnd(), )
  28.  
  29. PointCen().X = PointCen().X
  30.  
  31. PointCen().Y = PointCen().Y
  32.  
  33. Set PointElem = Application.CreatePointStringElement1(Nothing, PointCen, True)
  34.  
  35. ActiveModelReference.AddElement PointElem
  36.  
  37. If Sqr((PointCen().X - ) ^ + (PointCen().Y - ) ^ ) < Then
  38.  
  39. Num = Num +
  40.  
  41. End If
  42.  
  43. Next I
  44.  
  45. Dim MyCir As EllipseElement
  46.  
  47. Dim CenPt As Point3d
  48.  
  49. Dim RotMatrix As Matrix3d
  50.  
  51. CenPt.X =
  52.  
  53. CenPt.Y =
  54.  
  55. CenPt.Z =
  56.  
  57. Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, , , RotMatrix)
  58.  
  59. Application.ActiveModelReference.AddElement MyCir
  60.  
  61. Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, Sqr(), Sqr(), RotMatrix)
  62.  
  63. Application.ActiveModelReference.AddElement MyCir
  64.  
  65. MsgBox Num /
  66.  
  67. MsgBox Atn()
  1. Sub TestMessageBox2()
  2.  
  3. Dim Mes As VbMsgBoxResult
  4.  
  5. Mes = MsgBox("Unable to open file.", vbAbortRetryIgnore)
  6.  
  7. Mes = MsgBox("Format Hard Drive?", vbOKCancel)
  8.  
  9. Mes = MsgBox("New Level Added.", vbOKOnly)
  10.  
  11. Mes = MsgBox("Not Connected to Internet.", vbRetryCancel)
  12.  
  13. Mes = MsgBox("Do you want to continue?", vbYesNo)
  14.  
  15. Mes = MsgBox("Continue Reading File?", vbYesNoCancel)
  16.  
  17. Select Case Mes
  18.  
  19. Case VbMsgBoxResult.vbAbort
  20.  
  21. 'add codes
  22.  
  23. Case VbMsgBoxResult.vbCancel
  24.  
  25. 'add codes
  26.  
  27. Case VbMsgBoxResult.vbIgnore
  28.  
  29. 'add codes
  30.  
  31. Case VbMsgBoxResult.vbNo
  32.  
  33. 'add codes
  34.  
  35. Case VbMsgBoxResult.vbOK
  36.  
  37. 'add codes
  38.  
  39. Case VbMsgBoxResult.vbRetry
  40.  
  41. 'add codes
  42.  
  43. Case VbMsgBoxResult.vbYes
  44.  
  45. 'add codes
  46.  
  47. End Select
  48.  
  49. End Sub
  1.  
  1.  

  1. Sub TestMessageBox3()
  2.  
  3. Dim Mes As VbMsgBoxResult
  4.  
  5. Mes = MsgBox("Unable to open file.", vbAbortRetryIgnore + vbCritical)
  6.  
  7. Mes = MsgBox("Format Hard Drive?", vbOKCancel + vbExclamation)
  8.  
  9. Mes = MsgBox("New Level Added.", vbOKOnly + vbInformation)
  10.  
  11. Mes = MsgBox("Do you want to continue?", vbYesNo + vbQuestion)
  12.  
  13. End Sub

  1. Sub TestMessageBox4()
  2.  
  3. MsgBox "Testing Title", vbCritical, "Title Goes Here"
  4.  
  5. MsgBox "Testing Title", , "Title Goes Here"
  6.  
  7. End Sub

标题参数显示在消息框的顶部,它是第三个参数,消息框仅有一个参数是必须的,那就是提示参数。要显示提示、标题以及缺省按钮,在提示参数后方一个逗号、一个空格、另一个逗号,然后是标题。若要跳过一个可选参数,就让这个参数空着并用逗号指明你要提供下一个参数了。

输入框

  1. Sub TestInputBox2()
  2.  
  3. Dim InpRet As String
  4.  
  5. InpRet = InputBox("Enter Level Name:", "Level Creator", "Striping", , )
  6.  
  7. Debug.Print "User entered" & InpRet
  8.  
  9. End Sub

Now函数给出当前的系统日期和时间。

  1. Sub TestNow()
  2.  
  3. MsgBox Now
  4.  
  5. End Sub

DateAdd函数能够展望未来或回忆过去

  1. Sub TestDateAdd()
  2.  
  3. Dim NowDate As Date
  4.  
  5. NowDate = Now
  6.  
  7. Debug.Print NowDate & vbTab & DateAdd("d", , NowDate) '日
  8.  
  9. Debug.Print NowDate & vbTab & DateAdd("h", , NowDate) '
  10.  
  11. Debug.Print NowDate & vbTab & DateAdd("n", , NowDate) '分
  12.  
  13. Debug.Print NowDate & vbTab & DateAdd("s", , NowDate) '
  14.  
  15. Debug.Print NowDate & vbTab & DateAdd("m", , NowDate) '月
  16.  
  17. Debug.Print NowDate & vbTab & DateAdd("w", , NowDate) '
  18.  
  19. Debug.Print NowDate & vbTab & DateAdd("yyyy", , NowDate) '年
  20.  
  21. Debug.Print NowDate & vbTab & DateAdd("q", , NowDate) '
  22.  
  23. End Sub

DateDiff计算两个日期的时间差

  1. Sub TestDateDiff()
  2.  
  3. Dim NowDate As Date
  4.  
  5. NowDate = Now
  6.  
  7. Debug.Print "Days" & vbTab & DateDiff("d", NowDate, "1/1/3000")
  8.  
  9. Debug.Print "Hours" & vbTab & DateDiff("h", NowDate, "1/1/3000")
  10.  
  11. Debug.Print "Minutes" & vbTab & DateDiff("n", NowDate, "1/1/3000")
  12.  
  13. Debug.Print "Seconds" & vbTab & DateDiff("s", NowDate, "1/1/3000")
  14.  
  15. Debug.Print "Months" & vbTab & DateDiff("m", NowDate, "1/1/3000")
  16.  
  17. Debug.Print "Weeks" & vbTab & DateDiff("w", NowDate, "1/1/3000")
  18.  
  19. Debug.Print "Years" & vbTab & DateDiff("yyyy", NowDate, "1/1/3000")
  20.  
  21. Debug.Print "Quarters" & vbTab & DateDiff("q", NowDate, "1/1/3000")
  22.  
  23. End Sub

Timer 告诉我们自午夜开始所经历的秒数

  1. Sub TestTimer()
  2.  
  3. MsgBox Timer
  4.  
  5. End Sub

FileDataTime可以给出文件最后修改的日期/时间

FileLen函数可以告知给定文件的大小 以字节为单位

  1. Sub TestFileDateTime()
  2.  
  3. Dim exeDate As Date
  4.  
  5. exeDate = FileDateTime("C:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)\MicroStation\ustation.exe")
  6.  
  7. MsgBox "MicroStation Date/Time: " & exeDate
  8.  
  9. End Sub
  10.  
  11. Sub TestFileLen()
  12.  
  13. Dim exeSize As Long
  14.  
  15. exeSize = FileLen("C:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)\MicroStation\ustation.exe")
  16.  
  17. MsgBox "MicroStation Size: " & exeSize
  18.  
  19. End Sub

MkDir建立新目录

RmDir函数从文件系统中删除一个目录。被删除的目录必须是空的,否则会产生一个错误。

  1. Sub TestMkDir()
  2.  
  3. MkDir "c:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)\SourceCode"
  4.  
  5. End Sub
  6.  
  7. Sub TestRmDir()
  8.  
  9. RmDir "c:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)\SourceCode"
  10.  
  11. End Sub

Dir函数能够查找文件和文件夹(目录)

Kill 该函数的结果是永久性的。被“杀掉”的文件没有送到回收站,它们被完全删除。使用时要特别小心!

Beep函数使电脑发出哔哔声,在代码运行过程中它能给用户一个快速的声音提示。

SaveSetting使用Windows注册表能保存用户在软件中的设置。微软已经为VBA程序建立了一个注册表路径,我们很容易地写、编辑和删除这个路径

GetSetting函数能够取得注册表中的设置

  1. Sub TestSaveSetting()
  2.  
  3. SaveSetting "Learning MicroStation VBA", "Chapter 9", "SaveSetting", "It Works"
  4.  
  5. End Sub
  1. Sub TestGetSetting()
  2.  
  3. Dim RegSetting As String
  4.  
  5. RegSetting = GetSetting("Learning MicroStation VBA", "Chpater 9", "SaveSetting")
  6.  
  7. Debug.Print "The Key SaveSetting value is "" " & RegSetting & """"
  8.  
  9. End Sub

DeleteSetting能够删除注册表中的设置

  1. Sub TestDeleteSetting()
  2.  
  3. DeleteSetting "Learning Microstation VBA", "Chapter 9", "SaveSetting"
  4.  
  5. End Sub
  6.  
  7. Sub TestDeleteSetting2()
  8.  
  9. DeleteSetting "Learning Microstation VBA", "Chapter 9"
  10.  
  11. End Sub
  12.  
  13. Sub TestDeleteSetting3()
  14.  
  15. DeleteSetting "Learning MicroStation VBA"
  16.  
  17. End Sub

GetAllSettings函数从注册表中取得指定应用下的所有键,并把它们放入一个多维数组中

读写ASCII文件

  1. Sub TestWriteASCIIA()
  2.  
  3. Open "C:\output.txt" For Output As #
  4.  
  5. Print #, "First line"
  6.  
  7. Print #, "Second line"
  8.  
  9. Close #
  10.  
  11. End Sub
  12.  
  13. Sub TestWriteASCIIB()
  14.  
  15. Open "C:\output.txt" For Output As #
  16.  
  17. Write #, "First line"
  18.  
  19. Write #, "Second line"
  20.  
  21. Close #
  22.  
  23. End Sub
  24.  
  25. Sub TestWriteASCIIC()
  26.  
  27. Open "C:\output.txt" For Append As #
  28.  
  29. Print #, "Another line 1."
  30.  
  31. Print #, "Another line 2."
  32.  
  33. Close #
  34.  
  35. End Sub
  36.  
  37. Sub TestWriteASCIID()
  38.  
  39. Dim FFileA As Long
  40.  
  41. Dim FFileB As Long
  42.  
  43. FFileA = FFile
  44.  
  45. Open "C:\outputa.txt" For Append As #FFileA
  46.  
  47. Print #FFileA, "Another line 1."
  48.  
  49. Print #FFileA, "Another line 2."
  50.  
  51. FFileB = FreeFile
  52.  
  53. Open "C:outputb.txt" For Append As FFileB
  54.  
  55. Print #FFileA, "Another line 3."
  56.  
  57. Print #FFileB, "Another line 3."
  58.  
  59. Print #FFileA, "Another line 4."
  60.  
  61. Print #FFileB, "Another line 4."
  62.  
  63. Close #FFileB
  64.  
  65. Close #FFileA
  66.  
  67. End Sub
  68.  
  69. Sub ReadASCIIA()
  70.  
  71. Dim FFile As Long
  72.  
  73. Dim TextLine As String
  74.  
  75. FFile = FreeFile
  76.  
  77. Open "C:\output.txt" For Input As #FFile
  78.  
  79. While EOF(FFile) = False
  80.  
  81. Line Input #FFile, TextLine
  82.  
  83. Debug.Print TextLine
  84.  
  85. Wend
  86.  
  87. Close #FFile
  88.  
  89. End Sub

控制代码的执行

For…Next语句

P116

While …Wend

Do…Loop 可以使用Exit Do在任何时候退出Do…Loop语句

For Each…Next

If …Then还可以加上Else语句进行其他情况的处理,最后要加上End If

Select Case 相当于多个If语句

错误处理 On Error GoTo errhnd

On ErrorResume Next告诉VBA完全忽略错误并移动到下一行继续执行,从而代替去捕捉错误

errhand:

Select Case Err.number

Case 13 '类型不匹配

Err.Clear

'Resume Next

'Resume

End Select

本章回顾

VBA内置了很多过程和函数。使用内置的过程或者函数可以简化工作

第十章 可视界面

  1. Sub PrintHeader(HeaderIn As String, FileNum As Long, Optional Columns As Long = )
  2.  
  3. If optASCII.Value = True Then
  4.  
  5. Print #FileNum, "[" & HeaderIn & "]"
  6.  
  7. ElseIf optHTML.Value = True Then
  8.  
  9. Print #FileNum, "<table width=660>"
  10.  
  11. Print #FileNum, "<tr><td colspan=" & Columns & " align=center><b>" & HeaderIn & "</td></tr>"
  12.  
  13. End If
  14.  
  15. End Sub
  16.  
  17. Sub PrintLine(LineIn As String, FileNum As Long)
  18.  
  19. If optASCII.Value = True Then
  20.  
  21. Print #FileNum, LineIn
  22.  
  23. ElseIf optHTML.Value = True Then
  24.  
  25. Dim XSplit As Variant
  26.  
  27. Dim I As Long
  28.  
  29. XSplit = Split(LintIn, vbTab)
  30.  
  31. Print #FileNum, "<tr>"
  32.  
  33. For I = LBound(XSplit) To UBound(XSplit)
  34.  
  35. Print #FileNum, vbTab & "<td>" & XSplit(I) & "</td>"
  36.  
  37. Next I
  38.  
  39. Print #FileNum, "</tr>"
  40.  
  41. End If
  42.  
  43. End Sub
  44.  
  45. Sub PrintFooter(FileNum As Long)
  46.  
  47. If optHTML.Value = True Then
  48.  
  49. Print #FileNum, "</table>" & vbCrLf
  50.  
  51. End If
  52.  
  53. End Sub
  54.  
  55. Sub DoWriteFile()
  56.  
  57. frmWriteDgnSettings.Show
  58.  
  59. End Sub
  60.  
  61. Private Sub cmdCancel_Click()
  62.  
  63. Unload frmWriteDgnSettings
  64.  
  65. End Sub
  66.  
  67. Private Sub cmdOK_Click()
  68.  
  69. Dim MyFile As String
  70.  
  71. Dim FFile As Long
  72.  
  73. Dim myLevel As Level
  74.  
  75. Dim myLStyle As LineStyle
  76.  
  77. Dim myTStyle As TextStyle
  78.  
  79. Dim myView As View
  80.  
  81. FFile = FreeFile
  82.  
  83. If optASCII.Value = True Then
  84.  
  85. MyFile = "c:\output.txt"
  86.  
  87. ElseIf optHTML.Value = True Then
  88.  
  89. MyFile = "c:\output.html"
  90.  
  91. End If
  92.  
  93. Open MyFile For Output As #FFile
  94.  
  95. PrintHeader "FILE NAME", FFile,
  96.  
  97. PrintLine ActiveDesignFile.FullName, FFile
  98.  
  99. PrintFooter FFile
  100.  
  101. If chkLevels.Value = True Then
  102.  
  103. PrintHeader "LEVELS", FFile,
  104.  
  105. For Each myLevel In ActiveDesignFile.Levels
  106.  
  107. PrintLine myLevel.Name & vbTab & myLevel.Description & vbTab & myLevel.ElementColor, FFile
  108.  
  109. Next
  110.  
  111. PrintFooter FFile
  112.  
  113. End If
  114.  
  115. If chkLineStyles.Value = True Then
  116.  
  117. PrintHeader "LINE STYLES", FFile,
  118.  
  119. For Each myLStyle In ActiveDesignFile.LineStyles
  120.  
  121. PrintLine myLStyle.Name & vbTab & myLStyle.Number, FFile
  122.  
  123. Next
  124.  
  125. PrintFooter FFile
  126.  
  127. End If
  128.  
  129. If chkTextStyles.Value = True Then
  130.  
  131. PrintHeader "TEXT STYLES", FFile,
  132.  
  133. For Each myTStyle In ActiveDesignFile.TextStyles
  134.  
  135. PrintLine myTStyle.Name & vbTab & myTStyle.Color & vbTab & myTStyle.BackgroundFillColor, FFile
  136.  
  137. Next
  138.  
  139. PrintFooter FFile
  140.  
  141. End If
  142.  
  143. If chkViews.Value = True Then
  144.  
  145. PrintHeader "VIEWS", FFile,
  146.  
  147. For Each myView In ActiveDesignFile.Views
  148.  
  149. PrintLine myView.Origin.X & vbTab & myView.Origin.Y & vbTab & myView.Origin.Z & vbTab & myView.CameraAngle & vbTab & myView.CameraFocalLength, FFile
  150.  
  151. Next
  152.  
  153. PrintFooter FFile
  154.  
  155. End If
  156.  
  157. If chkAuthor.Value = True Then
  158.  
  159. PrintHeader "Authr", FFile
  160.  
  161. PrintLine ActiveDesignFile.Author, FFile
  162.  
  163. PrintFooter FFile
  164.  
  165. End If
  166.  
  167. If chkSubject.Value = True Then
  168.  
  169. PrintHeader "Subject", FFile
  170.  
  171. PrintLine ActiveDesignFile.Subject, FFile
  172.  
  173. PrintFooter FFile
  174.  
  175. End If
  176.  
  177. If chkTitle.Value = True Then
  178.  
  179. PrintHeader "Title", FFile
  180.  
  181. PrintLine ActiveDesignFile.Title, FFile
  182.  
  183. PrintFooter FFile
  184.  
  185. End If
  186.  
  187. Close #FFile
  188.  
  189. End Sub

VBA 操作数字的更多相关文章

  1. Excel VBA 操作 Word(入门篇)

    原文地址 本文的对象是:有一定Excel VBA基础,对Word VBA还没有什么认识,想在Excel中通过VBA操作Word还有困难的人.   一.新建Word引用 需要首先创建一个对 Word A ...

  2. Numeral.js – 格式化和操作数字的 JavaScript 库

    Numeral.js 是一个用于格式化和操作数字的 JavaScript 库.数字可以格式化为货币,百分比,时间,甚至是小数,千位,和缩写格式,功能十分强大.支持包括中文在内的17种语言. 您可能感兴 ...

  3. 转: 在.NET中操作数字证书

    作者:玄魂出处:博客2010-06-23 12:05 http://winsystem.ctocio.com.cn/19/9492019.shtml .NET为我们提供了操作数字证书的两个主要的类,分 ...

  4. 在.NET中操作数字证书(新手教程)

    .NET为我们提供了操作数字证书的两个主要的类,分为为: System.Security.Cryptography.X509Certificates.X509Certificate2类, 每个这个类的 ...

  5. Linux-Shell脚本编程-学习-4-Shell编程-操作数字-加减乘除计算

    对于任何一种编程语言都很重要的特性就是操作数字的能力,遗憾的是,对于shell脚本来说,这个过程比较麻烦,在shell脚本中有两种途径来进行数学运算操作. 1.expr 最开始的时候,shell提供了 ...

  6. VBA操作word生成sql语句

    项目开始一般都是用word保存下数据库的文档 但是从表单一个一个的建表实在是很困难乏味,查查资料 1.可以生成一个html或者xml,检索结构生成sql.但是这个方式也蛮麻烦 2.查到vba可以操作w ...

  7. js 操作数字类型

    1.内置函数 Number().parseInt().parseFloat() var num = "88.88abc888"; Number(num);              ...

  8. Oracle SQL语句操作数字:取整、四舍五入及格式化

    用oracle sql对数字进行操作: 取上取整.向下取整.保留N位小数.四舍五入.数字格式化 取整(向下取整): select floor(5.534) from dual;select trunc ...

  9. MicroStation VBA 操作提示

    Sub TestShowCommand() ShowCommand "画条线" ShowPrompt "选择第一个点" ShowStatus "选择第 ...

随机推荐

  1. 仿Office的程序载入窗体

    初次接触启动界面记不清是在哪一年了,估计是小学四年级第一次打开Office Word的时候吧,更记不清楚当时的启动界面是长啥样了.后来随着使用的软件越来越多,也见到各式各样的启动界面.下面就列举了两个 ...

  2. 常用Eclipse快捷方式

    Ctrl+1 快速修复 Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+↓ 当前行和下面一行交互位置(特 ...

  3. C#编程总结(二)多线程基础

    C#编程总结(二)多线程基础 无论您是为具有单个处理器的计算机还是为具有多个处理器的计算机进行开发,您都希望应用程序为用户提供最好的响应性能,即使应用程序当前正在完成其他工作.要使应用程序能够快速响应 ...

  4. Oracle中经典分页代码!

    在Oracle中因为没有top关键字,所以在sqlserver中的分页代码并不适用于Oracle,那么在Oracle中如何来实现分页呢? --查询所有数据 STUNO STUNAME STUAGE S ...

  5. Oracle中添加新用户并赋予权限

    --创建一个新用户NewUser 并设置密码为1 create user NewUser identified by 1; --为该用户赋予权限 grant connect , Resource to ...

  6. Tigase集群设置

    在两台Linux服务器ddd(IP:10.3.93.212)和tsung213(IP:10.3.93.213)上安装了Tigase,要把这两台服务器组成一个Tigase集群,只需要修改这两台服务器的i ...

  7. 【Java每日一题】20161123

    package Nov2016; import java.util.Scanner; public class Ques1123 { public static void main(String[] ...

  8. java Servlet(续)

    一.web.xml配置 由于需用户需要通过URL访问架设好的Servlet,所以我们必须将servlet映射到一个URL地址上, 比如上面的配置文件我们就可以通过:http://localhost:8 ...

  9. python统计列表内元素个数

    代码如下: list01 = ['a','b','c','a','c'] set01 = set(list01) print(set01) dict01 = {} for item in set01: ...

  10. MyEclipse10 中的两种FreeMarker插件的安装与配置

    第一个插件是:                                       freemarker-ide MyEclipce10.0中安装FreeMarker插件,这绝对是最简单的方法 ...