了解VBA基础

实习笔记1

2016年8月1日

14:12

Option Explicit

缺省情况下,如果使用一个没有声明的变量,它将继承“Variant”类型。在模块、窗体和类的通用声明区使用“OptionExplicit”能强制我们必须声明变量后才能使用变量

Sample:

Option Explicit

Sub test()

X = 5

End Sub

 

在通用声明区声明了“Option Explicit”。当试着去运行上述test宏时将得到一个错误

本章回顾

1.在过程、函数或用户窗体事件中写代码

2.在过程和函数中利用必须的和可选的参数

3.函数可返回值、数组、类型和对象

4.在过程和函数中,如果参数声明为“ByRef”,则可以改变作为参数传递进来的变量。把参数声明为“ByVal”将保持变量不变

4.能在过程、函数和事件中或者在通用声明区内声明变量。这些变量的范围依赖于它们在何处被声明以及声明中所用的关键字

第六章 变量

Option Explicit

Sub tst()

TTT = 5

MsgBox TTT

End Sub

'整型

Dim PageNumber As Integer

PageNumber = 84

'长整型

Dim MySalary As Long

MySalary = 123456

'双精度型

Dim HourToLearnVBA As Double

HourToLearnVBA = 36.25

Sub VariableTestC()

Dim N As Double

N = 1.23456789012346

N = 12345678901234.6

End Sub

'布尔类型

Dim ICanLearnThis As Boolean

ICanLearnThis = True

'日期型

Dim XMReleaseDate As Date

XMReleaseDate = "5/19/2006 8:00:00 AM"

'字符型

Dim MYLevelName As String

MYLevelName = "utilElectricity"

'对象型

Dim MyExcelApp As Object

Set MyExcelApp = GetObject(, "Excel.Application")

'Variant

Dim PointArray As Variant

'MicroStation特有的变量类型

'Application(应用)

Dim MSAPP As Application

Set MSAPP = Application

'ActiveDesignFile当前设计文件

'ActiveModelReference当前模型参考

'ActiveSettings 当前设置

'VBProject object & attributes 对象以及属性

'UserName用户名

'取得左、顶、宽和高属性

'DesignFile设计文件

Dim MyDGN As DesignFile

Set MyDGN = Application.ActiveDesignFile

'Author 作者,Client客户,Comments注释,Company公司,KeyWords关键字,Manager管理者,Subject主题,Title标题

'FormatMajorVersion 格式主版本 FormatMinorVersion格式次版本

'Levels层, Models模型, Name 名称,Path 路径

'ModelReference 模型参考

Dim MyModel As ModelReference

Set MyModel = Application.ActiveModelReference

'Level 层

Dim MyLevel As Level

Set MyLevel = Application.ActiveDesignFile.Levels(1)

'Description 描述, ElementColor元素颜色,ElementLineStyle元素线性,ElementLineWeight元素线宽,IsActive是否激活,IsDisplayed是否显示

'IsFrozen是否冻结,IsLocked是否锁定,Name名字,Number层号,Plot光栅绘图

'LineElement线元素

Dim MyLine As Application

Set MyLine = Application.CreateLineElement2(Nothing, Point3dFromXYZ(0, 0, 0), Point3dFromXYZ(4, 5, 6))

Application.ActiveModelReference.AddElement MyLine

'EllipseElement椭圆元素

Dim MyCircle As EllipseElement

Dim RotMatrix As Matrix3d

Set MyCircle = CreateEllipseElement2(Nothing, Point3dFromXYZ(0, 0, 0), 1.5, 1.5, RotMatrix)

Application.ActiveModelReference.AddElement MyCircle

'ArcElement 弧元素

Dim MyArc As ArcElement

Dim RotMatrix As Matrix3d

Set MyArc = CreateArcElement2(Nothing, Point3dFromXYZ(0, 0, 0), 1.75, 1.75, RotMatrix, Radians(45), Radiands(90))

Application.ActiveModelReference.AddElement MyArc

'TextElement文本元素

Dim MyText As TextElement

Dim RotMatrix As Matrix3d

Set MyText = CreateTextElement1(Nothing, "MicroStation VBA", Point3dFromXYZ(0, 0, 0), RotMatrix)

Application.ActiveModelReference.AddElement MyText

'赋值和设置对象

Dim LevelName As String

LevelName = "Element"

Dim EasementLevel As Level

Set EasementLevel = ActiveDesignFile.AddNewLevel(LevelName)

Dim StartPoint(0 To 2) As Double

StartPoint(0) = 4.5

StartPoint(1) = 5.6

StartPoint(2) = 6.7

Sub ArrayTestA()

Dim MyVerticies(0 To 1) As Point3d

Dim MyLine As LineElement

MyVerticies(0).X = 1

MyVerticies(0).Y = 2

MyVerticies(1).X = 3

MyVerticies(1).Y = 4

MyVerticies(2).X = 5

MyVerticies(2).Y = 6

Set MyLine = CreateLineElement1(Nothing, MyVerticies)

ActiveModelReference.AddElement MyLine

End Sub

Sub VariableTestD()

Dim MySalary As Double

Dim MySalary As Double

MySalary = 1234567

MyHourly = MySalary / 52 / 40

MsgBox "My Hourly Rate is " & FormatCurrency(MyHourly, 2, vbFalse, vbFalse, vbTrue)

End Sub

 

本章回顾:

变量是保存值或指向对象的名字。在函数、过程或事件内声明的变量是局部的,仅能在声明它的函数中使用,不能在函数外使用。在窗体或代码模块通用声明区中声明的变量能在声明它们的窗体或代码模块内任何地方使用。在代码模块中声明为“Public”的变量能在VBA工程的任何位置使用,在类模块中声明为“public”的变量成为类模块的读/写属性

如果没有变量,一切将是静态的什么也不能代表——线总是从固定的一点画到固定的另一点,文本总是插入在同一点且内容总是相同的。

第七章 操作文本

UCase大写转换

Function Ucase(String)

LCase小写转换

Function Lcase(String)

StrConv字符串转换

Function StrConv(String, Conversion As VbStrConv, [LocaleID AS Long])

WeekDayName星期名 WeekDay日期转星期

Function WeekDayName(Weekday As Long ,[Abbreviate As Boolean = False],[FirstDayOffWeek As VbDayOfWeek = vbUseSystemDayOfWeek]) As String

MonthNAme月名

Function MonthName(Month As Long, [Abbreviate As Boolean = False]) As String

LTrim左截取 RTrim右截取 Trim两端截取

Function LTrim(String)

Function RTrim(String)

Function Trim(String)

StrComp字符串比较

Function StrComp(String1, String2, [Compare As VbCompareMethod = vbBinaryCompare]

Left左取字符串

Function Left(String ,Length As Long)

Right右取字符串

Function Right(String, Length As Long)

Mid 取中串

Function Mid(String, Start As Long, [Length])

Sub TextWork12()

Dim BookTitle As String

BookTitle = "Learning MicroStation VBA"

Debug.Print Mid(BookTitle, 3, 6)

Debug.Print Mid(BookTitle, 6)

Debug.Print Mid(BookTitle, InStr(1, BookTitle, " ") + 1)

End Sub

 

'注意上面的InStr里面的" "里面是有一个空格

运行结果:

arning

ing MicroStation VBA

MicroStation VBA

第一个是从第三个字符开始并返回六个字符

第二个是从第六个字符开始并返回其后的每个字符

第三个是以Instr函数在变量BookTitle中查找第一个空格的位置并加上“1”作为起始,这样实际上是从第一个空格后开始的。如果没有指定长度,将得到空格后的所有内容

Replace 替换字符

Function Replace(Expression As String, Find As String, Replace As String, [Start As Long = 1],[Count As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare])AS String

Sub TextWork13()

Dim FilePath As String

Dim FilePath2 As String

FilePath = Application.ActiveDesignFile.FullName

FilePath2 = Replace(FilePath, "\", "//")

MsgBox FilePath & "turns into" & vbCr & FilePath2

End Sub

InStr 子串位置

Function InStr([Start],[String1],[String2],[Compare As VbCompareMethod = vbBinaryCompare])

InStrRev反向子串位置

Function InStrRev(StringCheck As String, StringMatch As String, [Start As Long = -1],[Compare As VbCompareMethod = vbBinaryCompare]) As Long

Split 分割和 Join联结

Function Split(Expression As String,[Delimiter],[Limit As Long = -1],[Compare As VbCompareMethod = vbBinaryCompare])

Function Join(SourceArray,[Delimiter]) As String

Asc 字符到ASCII码 和Chr ASCII码到字符

Function Asc(Stirng As String) As Integer

Function Chr(CharCode As Long)

FormatCurrency格式化货币

Function FormatCurrency(Expression, [NumDigitaAfterDecimal As Long = -1],[IncludeLeadingDigit As VbTriState = vbUseDefault],[GroupDigits As VbTriState = vbUseDefault])As String

FormatNumber格式化数字

Function FormatNumber(Expression,[NumDigitsAfterDecimal As Long =-1],[IncludeLeadingDigit As VbTriState = vbUseDefault],[UseParensForNegativenumbers As VbTriState = vbUseDefault],[GroupDigits As VbTriState = vbUseDefault]) As String

FormatDateTime格式化日期

Funcion FormatDateTime(Expression,[NamedFormat As VbDateTimeFormat = vbGeneralDate]) As String

Format格式化

Function Format(Expression, [Format],[FirstDayOfWeek As VbDayOfWeek = vbSunday], [FirstWeekOfYear As VbFirstWeekOfYear As VbFirstWeekOfYear = vbSunday],[FirstWeekOfYear As VbFirstWeekOfYear = vbFirstJan1])

 

&连接符

用来连接字符串

VbCr回车符<Enter>键

VbTab制表符<Tab>键

本章回顾:

字符串是指由文本、字母、数字和其他字符组合的单个文本块。本章集中讲述了如何操作这些字符串,例如大写转化,小写转化。连接字符串,格式化字符串,分割字符串等操作

<F8>可以单步

第8章 操作数字

加、减、乘、除、平方与指数(^2 或者^n)、平方根Sqr

作者: 伊甸一点

本文版权归作者伊甸一点和博客园所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.