赞
踩
所以application里的 workbook , worksheet range ,cell等应该是VBA语言的核心,最复杂内容了
-
- Sub test_wb11()
- '比较wb的名字 和 一般file的名字
-
-
- Dim fso1 As Object
- Dim fd1 As Object
-
- Set fso1 = CreateObject("scripting.filesystemobject")
- Set fd1 = fso1.GetFolder(ThisWorkbook.Path)
-
- For Each i In fd1.Files
- If i Like "*.xlsm" Then
- Debug.Print i.Name
- Debug.Print i.Path 'workbook工作簿的名字不一样
- Debug.Print i.Path & "\" & i.Name '这样做重复而多余
- ' Debug.Print i.FullName '会报错
- End If
- Next
- Debug.Print ""
-
- For Each j In Workbooks
- Debug.Print j.Name
- Debug.Print j.Path
- Debug.Print j.FullName 'wb有fullname属性
- Debug.Print j.Path & "\" & j.Name 'wb工作簿的fullname=path+ "" + name 是有意义的
-
- Next
-
- End Sub
Workbooks.Add 方法 新建一个工作表。新工作表将成为活动工作表。
语法
表达式.Add(Template)
表达式 一个代表 Workbooks 对象的变量。
- Sub test1_wb()
-
- '表达式.Add(Template)
- '确定如何创建新工作簿。如果此参数为指定现有 Microsoft Excel 文件名的字符串,那么创建新工作簿将以该指定的文件作为模板。如果此参数为常量,新工作簿将包含一个指定类型的工作表。
-
- Workbooks.Add 1
- Workbooks.Add (2)
-
- For i = 1 To 3
- Workbooks.Add
- Next
-
- End Sub
- Public Sub test_wb1()
-
- Debug.Print ThisWorkbook.Path
- Debug.Print ThisWorkbook.FullName
- Debug.Print ThisWorkbook.Name
-
-
- Application.Workbooks.Add
- ActiveWorkbook.SaveAs "C:\Users\Administrator\Desktop\t2.xls"
-
- Workbooks.Add
- ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & "t3.xls"
-
-
- End Sub
FN+F1帮助系统
Workbook.Save 方法
在另一不同文件中保存对工作簿所做的更改。(对新建的wb文件,是第一次保存)
workbook.saveas
表达式 一个代表 Workbook 对象的变量。
表达式.SaveAs(FileName, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout, Local)
- Public Sub test_wb2()
-
- Application.Workbooks.Add
- ActiveCell = 1
- ActiveWorkbook.Save
-
- Workbooks.Add
- ActiveCell = 11
- ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & "t2.xls"
-
- Workbooks.Add
- ActiveCell = 111
- ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & "t3.xls"
-
- End Sub
但实际上无所谓,比如保存为 .xls 文件,而实际EXCEL最新格式是 .xlsx,两者不一致所以警告
- Sub test_wb4()
-
- Workbooks.Add
- ActiveWorkbook.ActiveSheet.Range("a2") = a2 '错误例子,因为a2是变量,值为""
- ActiveWorkbook.ActiveSheet.Range("a3") = "a3"
- ActiveWorkbook.Save
- ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & "t4.xlsx"
- ActiveWorkbook.SaveCopyAs (ThisWorkbook.Path & "\" & "t5.xlsx")
-
- End Sub
- Public Sub test_wb3()
-
- path1 = ThisWorkbook.Path
-
- 'Application.DisplayAlerts = False '这个声明的生命周期也应该在sub内,有啥必要非在sub结尾前关闭掉?多此一举把
-
- Workbooks.Add
- '参数只有1个时,括号写法和非括号写法都可以
- ActiveWorkbook.SaveAs path1 & "\" & "t3.xlsx"
- ActiveWorkbook.SaveAs (path1 & "\" & "t3.xlsx")
- ActiveWorkbook.Close
- Workbooks.Add
- ActiveWorkbook.SaveAs path1 & "\" & "t2.xlsx", Password:=123
- ActiveWorkbook.SaveAs Filename:=path1 & "\" & "t2.xlsx", Password:=123
- '参数超过1个时,不能用括号写法,会报错
- 'ActiveWorkbook.SaveAs (path1 & "\" & "t2.xlsx", Password:=123)
- 'ActiveWorkbook.SaveAs (Filename:=path1 & "\" & "t2.xlsx", Password:=123)
- ActiveWorkbook.Close
-
-
- Workbooks.Open (path1 & "\" & "t3.xlsx")
- Workbooks("t3.xlsx").Close
-
- Workbooks.Open path1 & "\" & "t2.xlsx", Password:=123
- Workbooks("t2.xlsx").ActiveSheet.Range("a1") = "aaa"
- Workbooks("t2.xlsx").Save
- Workbooks("t2.xlsx").Close
-
- End Sub
- Public Sub test_wb3()
-
- path1 = ThisWorkbook.Path
-
-
- Workbooks.Add
- ActiveWorkbook.SaveAs path1 & "\" & "t2.xls"
- ActiveWorkbook.Close
- Workbooks.Add
- ActiveWorkbook.SaveAs path1 & "\" & "t3.xls"
- ActiveWorkbook.Close
-
-
- Workbooks("test1.xlsm").ActiveSheet.Range("a1") = "abc"
-
- Workbooks.Open path1 & "\" & "t2.xls"
- Workbooks("t2.xls").ActiveSheet.Range("a1") = "abc123111"
- Workbooks("t2.xls").Close
-
- Workbooks.Open path1 & "\" & "t3.xls"
- Workbooks("t3.xls").ActiveSheet.Range("a1") = "abcde12345111"
- Workbooks("t3.xls").Close
-
-
- End Sub
- Public Sub test_wb3()
-
- path1 = ThisWorkbook.Path
-
- Workbooks.Add
- ActiveWorkbook.SaveAs path1 & "\" & "t2.xls", Password:=123
- ActiveWorkbook.Close
- Workbooks.Add
- ActiveWorkbook.SaveAs path1 & "\" & "t3.xls", Password:=123
- ActiveWorkbook.Close
-
- Workbooks("test1.xlsm").ActiveSheet.Range("a1") = "abc"
-
- Workbooks.Open path1 & "\" & "t2.xls", Password:=123
- Workbooks("t2.xls").ActiveSheet.Range("a1") = "abc123111"
- Workbooks("t2.xls").Close
-
- Workbooks.Open path1 & "\" & "t3.xls", Password:=123
- Workbooks("t3.xls").ActiveSheet.Range("a1") = "abcde12345111"
- Workbooks("t3.xls").Close
-
- End Sub
- Public Sub test_wb3()
-
- path1 = ThisWorkbook.Path
-
- 'Application.DisplayAlerts = False '这个声明的生命周期也应该在sub内,有啥必要非在sub结尾前关闭掉?多此一举把
-
- Workbooks.Open path1 & "\" & "t2.xlsx", Password:=123
- Workbooks("t2.xlsx").ActiveSheet.Range("a1") = "aaa"
- Workbooks("t2.xlsx").Save
-
- Workbooks.Open path1 & "\" & "t2.xlsx", Password:=123
- Workbooks("t2.xlsx").ActiveSheet.Range("a2") = "bbb"
- Workbooks("t2.xlsx").Save
-
- Workbooks.Open path1 & "\" & "t2.xlsx", Password:=123
- Workbooks("t2.xlsx").ActiveSheet.Range("a3") = "cccc"
- Workbooks("t2.xlsx").Save
-
- End Sub
因为工作簿如何已经打开了
取内存中的工作簿,然后比较工作簿名字即可,找一个工作簿是否打开
现在版本的EXCEL已经不要求,文件不可重复打开了,所以这种检查不是很有必要。
- Sub test_wb5()
- '这样是取内存中的工作簿,已经打开的工作簿才能统计到
-
- For Each i In Application.Workbooks
- Debug.Print i.Name
- If i.Name = "" Then
- Debug.Print "xx工作簿已经打开"
- End If
- Next
-
- For j = 1 To Application.Workbooks.Count
- Debug.Print Workbooks(j).Name
- Next
-
- End Sub
-
- Sub test_wb6()
- '取某个文件夹下的工作簿数量
-
- Dim fso1 As Object
- Dim fd1 As Object
-
- Set fso1 = CreateObject("scripting.filesystemobject")
- Set fd1 = fso1.GetFolder(ThisWorkbook.Path)
-
- For Each i In fd1.Files
- If i Like "*.xlsm" Then
- Debug.Print i.Name
- ' i.Open '不支持这么打开
- End If
- Next
- End Sub
- Sub test_wb6()
- '取某个文件夹下的工作簿数量
-
- Dim fso1 As Object
- Dim fd1 As Object
-
- Set fso1 = CreateObject("scripting.filesystemobject")
- Set fd1 = fso1.GetFolder(ThisWorkbook.Path)
-
- For Each i In fd1.Files
- If i Like "*.xlsm" Then
-
- Workbooks.Open i.Path
- Workbooks(i.Path).ActiveSheet.Range("c1") = 666
-
- End If
- Next
- End Sub
- Sub test_wb7()
-
- 'dir不返回对象,返回字符串
- fd1_name = Dir(ThisWorkbook.Path, vbDirectory)
- Debug.Print fd1_name
-
- f1_name = Dir(ThisWorkbook.Path & "\" & "*.xlsm") '通配符查某类型文件
- Debug.Print f1_name '只查找一个文件
- '每次继续执行一次dir 指针会往下移动一次
-
- '没有条件变化的do loop ,是无限死循环
- 'Do While Not f1_name = ""
- ' Debug.Print f1_name
- 'Loop
-
- Do While Not f1_name = ""
- Debug.Print f1_name
- f1_name = Dir
- Loop
-
- End Sub
- Sub test_wb20()
-
-
- Workbooks.Add
- ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & "t2.xlsx"
- Workbooks("t2.xlsx").ActiveSheet.Range("c5") = 9999
- Workbooks("t2.xlsx").Close
-
- Workbooks.Open ThisWorkbook.Path & "\" & "t2.xlsx"
- Workbooks("t2.xlsx").ActiveSheet.Range("c6") = 100000
- Workbooks("t2.xlsx").Close savechanges:=True
-
-
- End Sub
- Sub test_wb21()
-
-
- Workbooks.Add
- ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & "t2.xlsx"
- Workbooks("t2.xlsx").ActiveSheet.Range("c5") = 9999
- Workbooks("t2.xlsx").Close
-
- Workbooks.Open ThisWorkbook.Path & "\" & "t2.xlsx"
- Workbooks("t2.xlsx").ActiveSheet.Range("c6") = 100000
- Workbooks("t2.xlsx").Close savechanges:=True, Filename:=ThisWorkbook.Path & "\" & "t3.xlsx"
-
-
- End Sub
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。