Use various objects in a VBA application objects are many exceptions, be careful
1.1 I now understand the level of VBA, this area can only be written this way, here to write the content is definitely a problem, no way, walking to school
- VBA in the special case of too much, really very pit
- But people really need to excel can not, we must use this
- applcaiton objects, workbook objects, sheet objects, worksheet objects, range objects, objects of various exceptions cell!
a.0 exception brief summary
- Only thisworkbook
- Only activesheet no activeworksheet
- activecell require a separate
Methods and attributes of objects a.1 VBA
- When in VBA editor
- Note that properties and methods vbe input object when the automatic display
- But there are exceptions, such as ActiveSheet object is an object, rather than the object range, input activesheet. Later will not automatically show the properties and methods
- As another example, a late-bound dictionary input dictionary. Not later display the corresponding methods and properties
a.2 VBA Object Browser
- Although it looks complicated, when needed, but also to rely on this, although I found some of the object manager there, enter the code window will complain of problems
- I am not quite understand
- But some objects properties and methods are not many, it is quite clear
- In short, these include the official F1 is an important basis for
a.3 VBE can be seen in several libraries
- Not commonly used library
- office library
- excel library
- Common Library
- VBA. Libraries' vba. Function?
- VBA.project library 'excel objects in daily management of wb, sh all resources such as libraries, the most commonly used
- msforms' form various libraries
- (Library where the dictionary, etc.) scripting '
1 EXCEL(一般情况下,少用excel.)
1.1 application 不是等价于 excel,Excel是库,Application是类
- Excel是库
- Application是类
- application是 excel的成员
Sub test_sh3()
Debug.Print Application.Name
Debug.Print Excel.Application.Name
'Debug.Print Excel.Name '报错
End Sub
1.2 excel 也有不少自身的. 属性 .方法
输入excel. 后面可用的属性和方法和 application 好像差不多
1.3 其他如 office 库,暂时也不用深究
2 Application (一般情况下,少用application. 用缺省的就行)
2.1 application的特点
2.2 application的属性和方法
- application.name
- application.activecell()
- application.activechart)
3 VBA.
3.1 VBA.下属的方法和属性
- Vba 代码中的函数,默认是VBA. 下的,缺省写就可以
- 而如果要调用工作表函数,需要用 application.worksheet.match() 和 application.match()
3.2 各种函数
- vba.match 等价于 match
- application.worksheetfunction.match 等价于 worksheetfunction.match
- application.match ( 和 worksheetfunction.match 这是有区别的)
--------------------------------------------------------------------------------------------------------------------------------
总结
一般情况下,少用excel.
一般情况下,少用application. 比如worksheets 就省略 application.worksheet ,少数地方必须用application.match 必须带
一般情况下,少用VBA. 默认 match 就是 vba.match 了解就行
这些特别大的 库,类,都没什么用,用缺省的写法感觉更简单、
-------------------------------------------------------------------------------------------------------------------------------
4 workbooks (workbook对象的集合,也就是workbook的类class)
正确
4.1 workbooks 是excel库下面的成员
4.2 具体用法
- workbooks 这几种都可以
- 等价于 Application.workbooks (这种情况下,application 少用)
- 等价于 excel.workbooks (少用excel. 没啥意义)
- 等价于 Excel.Application.Workbooks.Count (少用excel. 没啥意义)
Sub test551()
Debug.Print Excel.Application.Workbooks.Count
Debug.Print Excel.Workbooks.Count
Debug.Print Workbooks.Count
End Sub
5 workbook (也是excel下属的 class)
5.1 workbook
5.2 workbook的用法?
- workbooks("")
- 特定workbook的用法
5.3 特定workbook
正确
- thisworkbook
- activeworkbook
5.4 一个特别的用法( 没懂原理)
可以借助对象变量
Dim ws As Worksheet
If TypeName(ActiveSheet) = "Worksheet" Then Set ws = ActiveSheet
然后输入 ws. 自动列出成员
6 sheets (大于 worksheets,包含 worksheet chart 等等各种)
6.1 sheets 也是excel下的 class
7 worksheets (excel下属的class)
8 sheet
正确
- activesheet
- 因为 activesheet 不是 range,是对象,输入 activesheet. 后面是不会自动显示 属性,方法的
转化为变量后,输入 变量. 可以自动列出 对象和方法
Sub test1002()
Dim sh1 As Worksheet
Set sh1 = ActiveSheet
' 然后输入 sh1. 就可以了
End Sub
错误
- activeworksheet 没这种写法
- thissheet 代码都是保存在工作簿的,整理保存,不是保存在sheet里的
- sheet.save 工作簿都是整体保存的,不存在单独保存sheet
9 worksheet
workbook.worksheets()
下面这个好奇怪,怎么还有 range 下的worksheet
正确
错误
- activeworksheet
- thisworksheet
Sub test551()
Debug.Print ActiveSheet.Name
'Debug.Print ActiveworkSheet.Name ' 错误
End Sub
10 range
10.1一般区域 rang
10.2 特定区域
usedrange
11 selection
- selection 是比较宽泛的对象 object
- 可能是 cell rang
- 还可能是 worksheet 图表chart等
12 cells()
cells相关用法
- cells()
- activesheet.cells(1,1)
13 cell 特定cell
13.1 activecell
- activecell 只能单独使用
- 虽然确实其 parent 对象是 worksheet
正确
- activecell.value
错误
- activeworkbook.activesheet.activecell()
- activesheet.activecell()
- worksheets("sheet1").activecell()
测试代码
Sub test551()
Debug.Print ActiveCell.Parent.Name
Debug.Print ActiveCell.Parent.Parent.Name
Debug.Print ActiveCell.Parent.Parent.Parent.Name
Debug.Print ActiveCell
Debug.Print Sheet3.ActiveCell '虽然sheet3是其parent但是 activecell只能单独使用
Debug.Print Worksheets("sheet3").ActiveCell '虽然sheet3是其parent但是 activecell只能单独使用
End Sub