最近由于工作的原因,再次感受到了VBA在生成企业级报表时具有节省人力、提升质量的显著效果。觉得应该系统的学习一下VBA的理论知识了。首先,先来看看VBA有哪些关键字吧。
列表1. VBA关键字的分类及其描述(资料来源:Visual Basic Language Reference)
Category | 类别 | 描述(Description) |
Arrays | 数组 | Creating, defining, and using arrays. |
Compiler Directives | 编译器指令 | Controlling compiler behavior. |
Control Flow | 控制流 | Looping and controlling procedure flow. |
Conversion | 类型转换 | Converting numbers and data types. |
Data Types | 数据类型 | Data types and variant subtypes. |
Dates and Times | 日期及时间 | Converting and using date and time expressions. |
Directories and Files | 目录及文件 | Controlling the file system and processing files. |
Errors | 错误 | Trapping and returning error values. |
Financial | 财务 | Performing financial calculations. |
Input and Output | 输入输出 | Receiving input and displaying or printing output. |
Math | 数学 | Performing trigonometric and other mathematical calculations. |
Miscellaneous | 其它 | Starting other applications and processing events. |
Operators | 运算符 | Comparing expressions and other operations. |
String Manipulation | 字符串操作 | Manipulating strings and string type data. |
Variables and Constants | 变量及常数 | Declaring and defining variables and constants. |
列表2. VBA关键字按Action(用途)的分类
Category | Action | Keywords |
Arrays | Verify an array. | IsArray |
Arrays | Create an array. | Array |
Arrays | Change default lower limit. | Option Base |
Arrays | Declare and initialize an array. | Dim, Private, Public, ReDim, Static |
Arrays | Find the limits of an array. | LBound, UBound |
Arrays | Reinitialize an array. | Erase, ReDim |
Compiler Directives | Define compiler constant. | #Const |
Compiler Directives | Compile selected blocks of code. | #If...Then...#Else |
Control Flow | Branch. | GoSub...Return, GoTo, On Error, On...GoSub, On...GoTo |
Control Flow | Exit or pause the program. | DoEvents, End, Exit, Stop |
Control Flow | Loop. | Do...Loop, For...Next, For Each...Next, While...Wend, With |
Control Flow | Make decisions. | Choose, If...Then...Else, Select Case, Switch |
Control Flow | Use procedures. | Call, Function, Property Get, Property Let, Property Set, Sub |
Conversion | ANSI value to string. | Chr |
Conversion | String to lowercase or uppercase. | Format, LCase, Ucase |
Conversion | Date to serial number. | DateSerial, DateValue |
Conversion | Decimal number to other bases. | Hex, Oct |
Conversion | Number to string. | Format, Str |
Conversion | One data type to another. | CBool, CByte, CCur, CDate, CDbl, CDec, CInt, CLng, CLngLng, CLngPtr, CSng, CStr, CVar, CVErr, Fix, Int |
Conversion | Date to day, month, weekday, or year. | Day, Month, Weekday, Year |
Conversion | Time to hour, minute, or second. | Hour, Minute, Second |
Conversion | String to ASCII value. | Asc |
Conversion | String to number. | Val |
Conversion | Time to serial number. | TimeSerial, TimeValue |
Data Types | Convert between data types. | CBool, CByte, CCur, CDate, CDbl, CDec, CInt, CLng, CLngLng, CLngPtr, CSng, CStr, CVar, CVErr, Fix, Int |
Data Types | Set intrinsic data types. | Boolean, Byte, Currency, Date, Double, Integer, Long, LongLong, LongPtr, Object, Single, String, Variant (default) |
Data Types | Verify data types. | IsArray, IsDate, IsEmpty, IsError, IsMissing, IsNull, IsNumeric, IsObject |
Dates and Times | Get the current date or time. | Date, Now, Time |
Dates and Times | Perform date calculations. | DateAdd, DateDiff, DatePart |
Dates and Times | Return a date. | DateSerial, DateValue |
Dates and Times | Return a time. | TimeSerial, TimeValue |
Dates and Times | Set the date or time. | Date, Time |
Dates and Times | Time a process. | Timer |
Directories and Files | Change directory or folder. | ChDir |
Directories and Files | Change the drive. | ChDrive |
Directories and Files | Copy a file. | FileCopy |
Directories and Files | Make directory or folder. | MkDir |
Directories and Files | Remove directory or folder. | RmDir |
Directories and Files | Rename a file, directory, or folder. | Name |
Directories and Files | Return current path. | CurDir |
Directories and Files | Return file date/time stamp. | FileDateTime |
Directories and Files | Return file, directory, label attributes. | GetAttr |
Directories and Files | Return file length. | FileLen |
Directories and Files | Return file name or volume label. | Dir |
Directories and Files | Set attribute information for a file. | SetAttr |
Errors | Generate run-time errors. | Clear, Error, Raise |
Errors | Get error messages. | Error |
Errors | Provide error information. | Err |
Errors | Return Error variant. | CVErr |
Errors | Trap errors during run time. | On Error, Resume |
Errors | Type verification. | IsError |
Financial | Calculate depreciation. | DDB, SLN, SYD |
Financial | Calculate future value. | FV |
Financial | Calculate interest rate. | Rate |
Financial | Calculate internal rate of return. | IRR, MIRR |
Financial | Calculate number of periods. | NPer |
Financial | Calculate payments. | IPmt, Pmt, PPmt |
Financial | Calculate present value. | NPV, PV |
Input and Output | Access or create a file. | Open |
Input and Output | Close files. | Close, Reset |
Input and Output | Control output appearance. | Format, Print, Print #, Spc, Tab, Width # |
Input and Output | Copy a file. | FileCopy |
Input and Output | Get information about a file. | EOF, FileAttr, FileDateTime, FileLen, FreeFile, GetAttr, Loc, LOF, Seek |
Input and Output | Manage files. | Dir, Kill, Lock, Unlock, Name |
Input and Output | Read from a file. | Get, Input, Input #, Line Input # |
Input and Output | Return length of a file. | FileLen |
Input and Output | Set or get file attributes. | FileAttr, GetAttr, SetAttr |
Input and Output | Set read-write position in a file. | Seek |
Input and Output | Write to a file. | Print #, Put, Write # |
Math | Derive trigonometric functions. | Atn, Cos, Sin, Tan |
Math | General calculations. | Exp, Log, Sqr |
Math | Generate random numbers. | Randomize, Rnd |
Math | Get absolute value. | Abs |
Math | Get the sign of an expression. | Sgn |
Math | Perform numeric conversions. | Fix, Int |
Miscellaneous | Process pending events. | DoEvents |
Miscellaneous | Run other programs. | AppActivate, Shell |
Miscellaneous | Send keystrokes to an application. | SendKeys |
Miscellaneous | Sound a beep from computer. | Beep |
Miscellaneous | System. | Environ |
Miscellaneous | Provide a command-line string. | Command |
Miscellaneous | Automation. | CreateObject, GetObject |
Miscellaneous | Color. | QBColor, RGB |
Operators | Arithmetic. | ^, –, *, /, \, Mod, +, &, = |
Operators | Comparison. | =, <>, <, >, <=, >=, Like, Is |
Operators | Logical operations. | Not, And, Or, Xor, Eqv, Imp |
String Manipulation | Compare two strings. | StrComp |
String Manipulation | Convert strings. | StrConv |
String Manipulation | Convert to lowercase or uppercase. | Format, Lcase, Ucase |
String Manipulation | Create string of repeating character. | Space, String |
String Manipulation | Find length of a string. | Len |
String Manipulation | Format a string. | Format |
String Manipulation | Justify a string. | LSet, Rset |
String Manipulation | Manipulate strings. | InStr, Left, LTrim, Mid, Right, RTrim, Trim |
String Manipulation | Set string comparison rules. | Option Compare |
String Manipulation | Work with ASCII and ANSI values. | Asc, Chr |
Variables and Constants | Assign value. | Let |
Variables and Constants | Declare variables or constants. | Const, Dim, Private, Public, New, Static |
Variables and Constants | Declare module as private. | Option Private Module |
Variables and Constants | Get information about a variant. | IsArray, IsDate, IsEmpty, IsError, IsMissing, IsNull, IsNumeric, IsObject, TypeName, VarType |
Variables and Constants | Refer to current object. | Me |
Variables and Constants | Require explicit variable declarations. | Option Explicit |
Variables and Constants | Set default data type. | Deftype |