VBA Study Notes
2016-06-14 20:20
363 查看
One thing is important here is to distinguish the difference between VBA functions and Excel functions. In VBA code, you can use Application.Worksheetfunctions.xxx() to call excel functions. Also, here is the little difference between the functions' names:
VBA Excel
Rnd Rand
Sqr Sqrt
Log Ln
Here is some basic concepts in VBA:
VBA is also a oop program language. However, the concept of object is weaken here. Since what they refer as objects in VBA is actually a class. And we can straightly call the static function of different class to achieve different function. The common way
to obtain object is to use the text(String) name of the object.
For example, use sheets("sheetname") to obtain a real object of sheet. Also, we can use the index number in this situation of sheets.
如果有Option Explicit在module开头的话,所有的变量必须要先声明,再使用。
在VBA中声明变量的语法:
1、声明模块(module)级别或者过程(procedure)级别的变量:Dim temp As Integer
如果变量声明在module开头不属于任何一个函数或者sub的时候,属于一个module的全局变量。但是不能被同一个工程底下的不同的Module引用。
2、声明同一个工程底下的所有module都能用的变量:
Public strName As String
当然,以下这个总结十分到位(from http://blog.sina.com.cn/s/blog_60b6c5110100laxa.html):
1、Public和Private一般用于定义全局变量,也可以在类中使用。
1-1、区别在于:[Public]前者定义的是公共变量,如果在一个模块当中使用,那么整个应用程序都能使用它所定义的变量,如果在类中使用,那么它就是一个共有属性。
[Private]而后者定义的是私有变量,如果在一个模块中使用,那么只有这个模块才能访问到它所定义的变量,如果在类中使用,那么它就是一个私有属性。
2、Dim和Static一般在过程(Sub或者Function)内部使用,它们所定义的变量都只能在过程内部被访问。
2-1、区别在于:[Dim]前者定义的是动态变量,过程一旦结束,该变量所占有的内存就会被系统回收,而变量所储存的数据就会被破坏。
[Static]后者定义的是静态变量,这意味着在过程结束后这个变量所占有的内存不会被回收,数据当然也不会被破坏了,这样当你下次再调用该过程的时候,数据就依然存在。
相比之下,Public和Static都有保留数据不被破坏的作用,但是,前者适合于那些所有过程都可能访问到的变量,而后者则把变量的作用范围缩在最小(只在该过程内能被访问)。
VBA输出控制台:
1)Debug.print()输出到immediate 窗口
2) Message Box:
MsgBox 变量名
流程控制:
1) If then
If ActiveCell.Value < 0 Then
ActiveCell.Font.ColorIndex = 3
Else
ActiveCell.Font.ColorIndex = 4
End If
2) For Loop
For Num = 1 To 10
Total = Total + Num
Next Num
3)With … End With是excel VBA专有的,用来批量处理一个选择集合内的相同的objects
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
. WrapText = False
.Orientation = xlHorizontal
End With
等价的代码:
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.WrapText = False
Selection.Orientation = xlHorizontal
4)Select … Case … End Case 相当于 switch case
但不同的是,VBA没有break 但有Exit Select等跳出结构
https://msdn.microsoft.com/en-us/library/t2at9t47.aspx
否则case这个select语句会一直执行所有满足的case的语句
然后default 在VBA的代替是:
Select Case Condition
Case value_1
Code to Execute When Condition = value_1
Case value_2
Code to Execute When Condition = value_2
Case value_3
Code to Execute When Condition = value_3
Case Else
Code to Execute When all the other cases are False
End Select
使用Case Else
一些实践经验:
1、修改module name可以在properties 窗口进行,声明变量时候可以不声明类型
2、在自定义的函数里面可以引用excel本身的函数,引用方法是Applicaiton.ln(input)
char --字符 ‘a’ ‘b’ ‘1’ ASCII ‘A’ — 65 ‘B’ -66
String 字符串 “abcd” “””a"
Integer/Long
single/double -- 单精度浮点数/双精度 表示小数
32位内存和64位内存
variant
Byte 字节 1Byte = 8 bit
00000000(2^8)
Boolean (True false)
if(true/false)
表达式 a != 0 ==> Boolean
3、On error resume next就是抑制run time error,在这个条件下,如果该表不存在,Sheets("sheetsname").Name名字属性返回的是empty.
4、In VBA the concatenation operator is the & character, the same as Excel, but VBA also allows use of the + operator for concatenation of text strings.
5、调用函数的两种方法:按顺序赋值和按名字赋值(:=)
然后参数换行输入 (_)
6、调用private的procedure:
A procedure declared private can only be accessed from other procedures within the same module. Private sub procedures do not appear on the macro list in Excel. Private functions do not appear on the function list in Excel.
如果硬是要调用呢?
Application.Run(Macro_name,arg1 ...)
注意名字必须是:Module1.M1Macro to run the M1Macro in Module1 from another module
VBA Excel
Rnd Rand
Sqr Sqrt
Log Ln
Here is some basic concepts in VBA:
VBA is also a oop program language. However, the concept of object is weaken here. Since what they refer as objects in VBA is actually a class. And we can straightly call the static function of different class to achieve different function. The common way
to obtain object is to use the text(String) name of the object.
For example, use sheets("sheetname") to obtain a real object of sheet. Also, we can use the index number in this situation of sheets.
如果有Option Explicit在module开头的话,所有的变量必须要先声明,再使用。
在VBA中声明变量的语法:
1、声明模块(module)级别或者过程(procedure)级别的变量:Dim temp As Integer
如果变量声明在module开头不属于任何一个函数或者sub的时候,属于一个module的全局变量。但是不能被同一个工程底下的不同的Module引用。
2、声明同一个工程底下的所有module都能用的变量:
Public strName As String
当然,以下这个总结十分到位(from http://blog.sina.com.cn/s/blog_60b6c5110100laxa.html):
1、Public和Private一般用于定义全局变量,也可以在类中使用。
1-1、区别在于:[Public]前者定义的是公共变量,如果在一个模块当中使用,那么整个应用程序都能使用它所定义的变量,如果在类中使用,那么它就是一个共有属性。
[Private]而后者定义的是私有变量,如果在一个模块中使用,那么只有这个模块才能访问到它所定义的变量,如果在类中使用,那么它就是一个私有属性。
2、Dim和Static一般在过程(Sub或者Function)内部使用,它们所定义的变量都只能在过程内部被访问。
2-1、区别在于:[Dim]前者定义的是动态变量,过程一旦结束,该变量所占有的内存就会被系统回收,而变量所储存的数据就会被破坏。
[Static]后者定义的是静态变量,这意味着在过程结束后这个变量所占有的内存不会被回收,数据当然也不会被破坏了,这样当你下次再调用该过程的时候,数据就依然存在。
相比之下,Public和Static都有保留数据不被破坏的作用,但是,前者适合于那些所有过程都可能访问到的变量,而后者则把变量的作用范围缩在最小(只在该过程内能被访问)。
VBA输出控制台:
1)Debug.print()输出到immediate 窗口
2) Message Box:
MsgBox 变量名
流程控制:
1) If then
If ActiveCell.Value < 0 Then
ActiveCell.Font.ColorIndex = 3
Else
ActiveCell.Font.ColorIndex = 4
End If
2) For Loop
For Num = 1 To 10
Total = Total + Num
Next Num
3)With … End With是excel VBA专有的,用来批量处理一个选择集合内的相同的objects
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
. WrapText = False
.Orientation = xlHorizontal
End With
等价的代码:
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.WrapText = False
Selection.Orientation = xlHorizontal
4)Select … Case … End Case 相当于 switch case
但不同的是,VBA没有break 但有Exit Select等跳出结构
https://msdn.microsoft.com/en-us/library/t2at9t47.aspx
否则case这个select语句会一直执行所有满足的case的语句
然后default 在VBA的代替是:
Select Case Condition
Case value_1
Code to Execute When Condition = value_1
Case value_2
Code to Execute When Condition = value_2
Case value_3
Code to Execute When Condition = value_3
Case Else
Code to Execute When all the other cases are False
End Select
使用Case Else
一些实践经验:
1、修改module name可以在properties 窗口进行,声明变量时候可以不声明类型
2、在自定义的函数里面可以引用excel本身的函数,引用方法是Applicaiton.ln(input)
char --字符 ‘a’ ‘b’ ‘1’ ASCII ‘A’ — 65 ‘B’ -66
String 字符串 “abcd” “””a"
Integer/Long
single/double -- 单精度浮点数/双精度 表示小数
32位内存和64位内存
variant
Byte 字节 1Byte = 8 bit
00000000(2^8)
Boolean (True false)
if(true/false)
表达式 a != 0 ==> Boolean
3、On error resume next就是抑制run time error,在这个条件下,如果该表不存在,Sheets("sheetsname").Name名字属性返回的是empty.
4、In VBA the concatenation operator is the & character, the same as Excel, but VBA also allows use of the + operator for concatenation of text strings.
5、调用函数的两种方法:按顺序赋值和按名字赋值(:=)
然后参数换行输入 (_)
6、调用private的procedure:
A procedure declared private can only be accessed from other procedures within the same module. Private sub procedures do not appear on the macro list in Excel. Private functions do not appear on the function list in Excel.
如果硬是要调用呢?
Application.Run(Macro_name,arg1 ...)
注意名字必须是:Module1.M1Macro to run the M1Macro in Module1 from another module
相关文章推荐
- 使用Python生成Excel格式的图片
- Outlook 批量发送邮件
- 穿越到 1983 年学习 BASIC
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- FREEBASIC 编译可被python调用的dll函数示例
- VBA将excel数据表生成JSON文件
- excel vba 限制工作表的滚动区域代码
- VBA解决Windows空当接龙的617局
- excel vba 高亮显示当前行代码
- ColdFusionMX 编程指南 ColdFusionMX Basic Tag编程
- SQL 导入导出Excel数据的语句
- VBA中连接SQLSERVER数据库例子
- 文本、Excel、Access数据导入SQL Server2000的方法
- C#实现Excel动态生成PivotTable
- C#导出数据到Excel文件的方法
- Vbscript生成Excel报表的常用操作总结
- python pandas 处理 excel
- 利用outlook结合word与excel实现批量发送邮件