Excel中VBA编程学习笔记(⼋)--⼯作薄应⽤
(WORKBOOK)
1、获取⼯作薄数量
Private Sub test()
MsgBox ("⼯作薄数量为:" & Workbooks.Count)
Workbooks(1).Activate
Workbooks("第6次作业成绩.xls").Activate
MsgBox ("当前⼯作薄名:" + ThisWorkbook.name)
End Sub
注:Workbooks(1)表⽰第⼀个⼯作薄,访问某个⼯作薄可以通过下表索引也可以通过名称。ThisWorkbook表⽰当前⼯作薄,执⾏VBA 代码所在的⼯作薄,ActivateWorkBook表⽰活动⼯作薄,它们可能相同也可能不同。
2、遍历所有⼯作薄
Sub test()
For Each wb In Workbooks
Debug.Print wb.Name
Next
For index = 1 To Windows.Count
Debug.Print Windows(index).Parent.Name
Next
End Sub
注意:上⾯两种⽅式输出的结果的顺序不同
3、删除⼯作薄中的所有图表
Private Sub test()
ThisWorkbook.Charts.Delete
End Sub
4、设置⼯作薄为⾃动更新
Private Sub test()
ThisWorkbook.AutoUpdateSaveChanges = True
ThisWorkbook.AutoUpdateFrequency = 5 '以分钟为单位
End Sub
注意:此时必须设置“共享⼯作薄”,并将⼯作薄保存成xlsm格式的共享⼯作薄。
5、获取⼯作薄名称及全路径
Private Sub test()
Dim name, fullname As String
name = ThisWorkbook.name  '⼯作薄名
fullname = ThisWorkbook.fullname  '⼯作薄全名,包括磁盘路劲End Sub
注:在⼯作薄为保存之前,上⾯两个值相同。
6、检查⼯作薄是否存在宏项⽬
Private Sub test()
If ThisWorkbook.HasVBProject = True Then
MsgBox ("包含宏项⽬")
Else
MsgBox ("不包含宏项⽬")
End If
End Sub
7、检查⼯作薄是否以只读⽅式打开
Private Sub test()
If ThisWorkbook.ReadOnly = True Then
MsgBox ("只读⽅式打开")
Else
MsgBox ("⾮只读⽅式打开")
End If
End Sub
8、获取当前⼯作薄中所有⼯作表名称
Private Sub test()
Dim i As Integer
Dim str As String
str = ""
For i = 1 To ThisWorkbook.Sheets.Count        ‘遍历每个⼯作表
str = str & ThisWorkbook.Sheets(i).name & ";"
Next
MsgBox (str)
End Sub
9、为⼯作薄设置密码
Private Sub test()
ThisWorkbook.Password = "123456"  '设置打开密码
ThisWorkbook.WritePassword = "123" '设置写密码
End Sub
10、打印⼯作薄前3页
Private Sub test()
ThisWorkbook.PrintOut form:=1, to:=3
End Sub
11、⼯作薄的保护
Private Sub test()
ThisWorkbook.Protect Password = "123456", structure:=True, Windows:=True
ThisWorkbook.Unprotect '取消保护
End Sub
12、打开、新建及保存⼯作薄
Private Sub test()
ThisWorkbook.Save  '保存更改
ThisWorkbook.SaveAs Filename:=Application.GetSaveAsFilename, Password:="123456"  ‘另存为End Sub
Sub test()
Application.DisplayAlerts = False '不出现提⽰框
Set wk = Workbooks.Add  '新建⼯作薄
wk.Sheets(1).[a1].value = 122
Dim name$
name = ThisWorkbook.Path & "\新建⼯作薄.xls"
wk.SaveAs Filename:=name, FileFormat:=xlWorkbookNormal '另存为
wk.Close    '关闭新建的⼯作薄
Application.DisplayAlerts = True
Workbooks.Open name
Debug.Print ActiveWorkbook.Sheets(1).Range("A1").value  '新打开的⼯作薄为激活的⼯作薄
Debug.Print Workbooks(2).Sheets(1).Range("A1").value    '因为已经存在⼀个⼯作薄,再打开⼯作薄时为⼯作薄2
Workbooks(2).Close
End Sub
在打开⼯作薄时候有时候会提⽰更新链接的消息框,如果不想显⽰这些提⽰信息,可以在打开设置参数UpdateLinks值为0,例如:WorksBooks.Open Filname:=”test.xls”,UpdateLinks:=0
UpdateLinks参数取值及含义
取值含义取值含义
0不更新任何引⽤1更新外部引⽤,但不更新远程引⽤
2更新远程引⽤,但不更新外部引⽤3同时更新外部引⽤及远程引⽤
13、遍历当前⽂件夹下的所有⼯作薄
下⾯程序获取当前⽂件夹下⾯所有的.xls⽂件,并将⽂件名保存到数组arrayFile
Dim arrayFile(1 To 100) As String
Count = 1
Path = ThisWorkbook.Path '获取当前路径
resultFile = ThisWorkbook.Name
myfile = Dir(Path & "\*.xls")  '选中所有的.xls⽂件
Do
If myfile <> ThisWorkbook.Name Then
arrayFile(Count) = myfile
vba自学好学吗
Count = Count + 1
End If
myfile = Dir '选中下⼀个⽂件
Loop While myfile <> ""
14、⼯作薄的各种事件
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MsgBox ("即将关闭⼯作薄")
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)    MsgBox ("保存更改")
End Sub
Private Sub Workbook_Open()
MsgBox ("您打开了⼯作薄:" & ThisWorkbook.FullName)
End Sub
Private Sub Workbook_WindowResize(ByVal Wn As Window)
MsgBox ("改变⼯作薄⼤⼩")
End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。