第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 的类型声明字符是数字符号 (#)。

Sub TestRnd()

Dim I As Long

Dim Lower As Long

Dim Higher As Long

Dim PointCen( To ) As Point3d

Dim PointElem As PointStringElement

Dim Num As Double

Lower = 

Higher = 

Num = 

Randomize

For I =  To 

PointCen().X = Round((Higher - Lower + ) * Rnd(), )

PointCen().Y = Round((Higher - Lower + ) * Rnd(), )

PointCen().X = PointCen().X

PointCen().Y = PointCen().Y

Set PointElem = Application.CreatePointStringElement1(Nothing, PointCen, True)

ActiveModelReference.AddElement PointElem

If Sqr((PointCen().X - ) ^  + (PointCen().Y - ) ^ ) <  Then

Num = Num + 

End If

Next I

Dim MyCir As EllipseElement

Dim CenPt As Point3d

Dim RotMatrix As Matrix3d

CenPt.X = 

CenPt.Y = 

CenPt.Z = 

Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, , , RotMatrix)

Application.ActiveModelReference.AddElement MyCir

Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, Sqr(), Sqr(), RotMatrix)

Application.ActiveModelReference.AddElement MyCir

MsgBox Num / 

MsgBox Atn()
Sub TestMessageBox2()

Dim Mes As VbMsgBoxResult

Mes = MsgBox("Unable to open file.", vbAbortRetryIgnore)

Mes = MsgBox("Format Hard Drive?", vbOKCancel)

Mes = MsgBox("New Level Added.", vbOKOnly)

Mes = MsgBox("Not Connected to Internet.", vbRetryCancel)

Mes = MsgBox("Do you want to continue?", vbYesNo)

Mes = MsgBox("Continue Reading File?", vbYesNoCancel)

Select Case Mes

Case VbMsgBoxResult.vbAbort

'add codes

Case VbMsgBoxResult.vbCancel

'add codes

Case VbMsgBoxResult.vbIgnore

'add codes

Case VbMsgBoxResult.vbNo

'add codes

Case VbMsgBoxResult.vbOK

'add codes

Case VbMsgBoxResult.vbRetry

'add codes

Case VbMsgBoxResult.vbYes

'add codes

End Select

End Sub

 

Sub TestMessageBox3()

Dim Mes As VbMsgBoxResult

Mes = MsgBox("Unable to open file.", vbAbortRetryIgnore + vbCritical)

Mes = MsgBox("Format Hard Drive?", vbOKCancel + vbExclamation)

Mes = MsgBox("New Level Added.", vbOKOnly + vbInformation)

Mes = MsgBox("Do you want to continue?", vbYesNo + vbQuestion)

End Sub

Sub TestMessageBox4()

MsgBox "Testing Title", vbCritical, "Title Goes Here"

MsgBox "Testing Title", , "Title Goes Here"

End Sub

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

输入框

Sub TestInputBox2()

Dim InpRet As String

InpRet = InputBox("Enter Level Name:", "Level Creator", "Striping", , )

Debug.Print "User entered" & InpRet

End Sub

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

Sub TestNow()

MsgBox Now

End Sub

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

Sub TestDateAdd()

Dim NowDate As Date

NowDate = Now

Debug.Print NowDate & vbTab & DateAdd("d", , NowDate) '日

Debug.Print NowDate & vbTab & DateAdd("h", , NowDate) '时

Debug.Print NowDate & vbTab & DateAdd("n", , NowDate) '分

Debug.Print NowDate & vbTab & DateAdd("s", , NowDate) '秒

Debug.Print NowDate & vbTab & DateAdd("m", , NowDate) '月

Debug.Print NowDate & vbTab & DateAdd("w", , NowDate) '周

Debug.Print NowDate & vbTab & DateAdd("yyyy", , NowDate) '年

Debug.Print NowDate & vbTab & DateAdd("q", , NowDate) '季

End Sub

DateDiff计算两个日期的时间差

Sub TestDateDiff()

Dim NowDate As Date

NowDate = Now

Debug.Print "Days" & vbTab & DateDiff("d", NowDate, "1/1/3000")

Debug.Print "Hours" & vbTab & DateDiff("h", NowDate, "1/1/3000")

Debug.Print "Minutes" & vbTab & DateDiff("n", NowDate, "1/1/3000")

Debug.Print "Seconds" & vbTab & DateDiff("s", NowDate, "1/1/3000")

Debug.Print "Months" & vbTab & DateDiff("m", NowDate, "1/1/3000")

Debug.Print "Weeks" & vbTab & DateDiff("w", NowDate, "1/1/3000")

Debug.Print "Years" & vbTab & DateDiff("yyyy", NowDate, "1/1/3000")

Debug.Print "Quarters" & vbTab & DateDiff("q", NowDate, "1/1/3000")

End Sub

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

Sub TestTimer()

MsgBox Timer

End Sub

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

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

Sub TestFileDateTime()

Dim exeDate As Date

exeDate = FileDateTime("C:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)\MicroStation\ustation.exe")

MsgBox "MicroStation Date/Time: " & exeDate

End Sub

Sub TestFileLen()

Dim exeSize As Long

exeSize = FileLen("C:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)\MicroStation\ustation.exe")

MsgBox "MicroStation Size: " & exeSize

End Sub

MkDir建立新目录

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

Sub TestMkDir()

MkDir "c:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)\SourceCode"

End Sub

Sub TestRmDir()

RmDir "c:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)\SourceCode"

End Sub

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

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

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

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

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

Sub TestSaveSetting()

SaveSetting "Learning MicroStation VBA", "Chapter 9", "SaveSetting", "It Works"

End Sub
Sub TestGetSetting()

Dim RegSetting As String

RegSetting = GetSetting("Learning MicroStation VBA", "Chpater 9", "SaveSetting")

Debug.Print "The Key SaveSetting value is "" " & RegSetting & """"

End Sub

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

Sub TestDeleteSetting()

DeleteSetting "Learning Microstation VBA", "Chapter 9", "SaveSetting"

End Sub

Sub TestDeleteSetting2()

DeleteSetting "Learning Microstation VBA", "Chapter 9"

End Sub

Sub TestDeleteSetting3()

DeleteSetting "Learning MicroStation VBA"

End Sub

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

读写ASCII文件

Sub TestWriteASCIIA()

Open "C:\output.txt" For Output As #

Print #, "First line"

Print #, "Second line"

Close #

End Sub

Sub TestWriteASCIIB()

Open "C:\output.txt" For Output As #

Write #, "First line"

Write #, "Second line"

Close #

End Sub

Sub TestWriteASCIIC()

Open "C:\output.txt" For Append As #

Print #, "Another line 1."

Print #, "Another line 2."

Close #

End Sub

Sub TestWriteASCIID()

Dim FFileA As Long

Dim FFileB As Long

FFileA = FFile

Open "C:\outputa.txt" For Append As #FFileA

Print #FFileA, "Another line 1."

Print #FFileA, "Another line 2."

FFileB = FreeFile

Open "C:outputb.txt" For Append As FFileB

Print #FFileA, "Another line 3."

Print #FFileB, "Another line 3."

Print #FFileA, "Another line 4."

Print #FFileB, "Another line 4."

Close #FFileB

Close #FFileA

End Sub

Sub ReadASCIIA()

Dim FFile As Long

Dim TextLine As String

FFile = FreeFile

Open "C:\output.txt" For Input As #FFile

While EOF(FFile) = False

Line Input #FFile, TextLine

Debug.Print TextLine

Wend

Close #FFile

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内置了很多过程和函数。使用内置的过程或者函数可以简化工作

第十章 可视界面

Sub PrintHeader(HeaderIn As String, FileNum As Long, Optional Columns As Long = )

If optASCII.Value = True Then

Print #FileNum, "[" & HeaderIn & "]"

ElseIf optHTML.Value = True Then

Print #FileNum, "<table width=660>"

Print #FileNum, "<tr><td colspan=" & Columns & " align=center><b>" & HeaderIn & "</td></tr>"

End If

End Sub

Sub PrintLine(LineIn As String, FileNum As Long)

If optASCII.Value = True Then

Print #FileNum, LineIn

ElseIf optHTML.Value = True Then

Dim XSplit As Variant

Dim I As Long

XSplit = Split(LintIn, vbTab)

Print #FileNum, "<tr>"

For I = LBound(XSplit) To UBound(XSplit)

Print #FileNum, vbTab & "<td>" & XSplit(I) & "</td>"

Next I

Print #FileNum, "</tr>"

End If

End Sub

Sub PrintFooter(FileNum As Long)

If optHTML.Value = True Then

Print #FileNum, "</table>" & vbCrLf

End If

End Sub

Sub DoWriteFile()

frmWriteDgnSettings.Show

End Sub

Private Sub cmdCancel_Click()

Unload frmWriteDgnSettings

End Sub

Private Sub cmdOK_Click()

Dim MyFile As String

Dim FFile As Long

Dim myLevel As Level

Dim myLStyle As LineStyle

Dim myTStyle As TextStyle

Dim myView As View

FFile = FreeFile

If optASCII.Value = True Then

MyFile = "c:\output.txt"

ElseIf optHTML.Value = True Then

MyFile = "c:\output.html"

End If

Open MyFile For Output As #FFile

PrintHeader "FILE NAME", FFile, 

PrintLine ActiveDesignFile.FullName, FFile

PrintFooter FFile

If chkLevels.Value = True Then

PrintHeader "LEVELS", FFile, 

For Each myLevel In ActiveDesignFile.Levels

PrintLine myLevel.Name & vbTab & myLevel.Description & vbTab & myLevel.ElementColor, FFile

Next

PrintFooter FFile

End If

If chkLineStyles.Value = True Then

PrintHeader "LINE STYLES", FFile, 

For Each myLStyle In ActiveDesignFile.LineStyles

PrintLine myLStyle.Name & vbTab & myLStyle.Number, FFile

Next

PrintFooter FFile

End If

If chkTextStyles.Value = True Then

PrintHeader "TEXT STYLES", FFile, 

For Each myTStyle In ActiveDesignFile.TextStyles

PrintLine myTStyle.Name & vbTab & myTStyle.Color & vbTab & myTStyle.BackgroundFillColor, FFile

Next

PrintFooter FFile

End If

If chkViews.Value = True Then

PrintHeader "VIEWS", FFile, 

For Each myView In ActiveDesignFile.Views

PrintLine myView.Origin.X & vbTab & myView.Origin.Y & vbTab & myView.Origin.Z & vbTab & myView.CameraAngle & vbTab & myView.CameraFocalLength, FFile

Next

PrintFooter FFile

End If

If chkAuthor.Value = True Then

PrintHeader "Authr", FFile

PrintLine ActiveDesignFile.Author, FFile

PrintFooter FFile

End If

If chkSubject.Value = True Then

PrintHeader "Subject", FFile

PrintLine ActiveDesignFile.Subject, FFile

PrintFooter FFile

End If

If chkTitle.Value = True Then

PrintHeader "Title", FFile

PrintLine ActiveDesignFile.Title, FFile

PrintFooter FFile

End If

Close #FFile

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. Java总结篇系列:Java多线程(三)

    本文主要接着前面多线程的两篇文章总结Java多线程中的线程安全问题. 一.一个典型的Java线程安全例子 public class ThreadTest { public static void ma ...

  2. 深度技术32位Win7系统Ghost版2014年

    深度技术32位Win7系统Ghost版,GhostWin7是指使用Ghost软件做成压缩包的Windows7,俗称克隆版Win7.用克隆版的目的是节省安装时间.本作品在采用微软封装部署技术的基础上,结 ...

  3. 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】---暑假三校训练

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D Problem D. Distribution in Metagonia Input ...

  4. Java--Exchanger用于进行线程间的数据交换

    package com; import java.util.concurrent.Exchanger; /** * Created by yangyu on 16/11/28. */ /** * Ex ...

  5. AWS CloudFront CDN直接全站加速折腾记The request could not be satisfied. Bad request

    ERROR The request could not be satisfied. Bad request. Generated by cloudfront (CloudFront) Request ...

  6. Quartz任务调度器

    在使用jdk的timer时发现无法满足开发需求:即无法在指定的日期进行执行任务. 这便引入一个优秀的开源任务调度框架“quartz”.   这里加入的是quartz-1.8.6版本.Quart的官网: ...

  7. ADO.NET操作数据库(一)

    ---恢复内容开始--- [1]ADO.Net简介2015-12-07-20:16:05 ADO.Net提供对Microsoft SQL Server数据源以及通过OLE DB和XML公开的数据源的一 ...

  8. Quartz.NET开源作业调度框架系列(一):快速入门step by step

    Quartz.NET是一个被广泛使用的开源作业调度框架 , 由于是用C#语言创建,可方便的用于winform和asp.net应用程序中.Quartz.NET提供了巨大的灵活性但又兼具简单性.开发人员可 ...

  9. Progress.js – 为页面上的任意对象创建进度条效果

    Progress.js 是一个 JavaScript 和 CSS3 的库,它帮助开发人员为网页上的每个对象创建和管理进度条效果.你可以设计自己的模板,进度条或者干脆定制. 您可以使用 Progress ...

  10. Click Magick – 下一代点击跟踪和链接管理

    Click Magick 是新一代的广告跟踪和链接管理系统,让每一个点击都能给你带去更多的利润.它是专门设计来跟踪所有类型的点击计费广告,包括从谷歌,必应和 Facebook 的 PPC 广告,就好像 ...