實習筆記1 2016年8月1日 14:12 Option Explicit 預設情況下,如果使用一個沒有聲明的變數,它將繼承“Variant”類型。在模塊、窗體和類的通用聲明區使用“OptionExplicit”能強制我們必須聲明變數後才能使用變數 Sample: 在通用聲明區聲明瞭“Option ...
實習筆記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