我如何打开所有的Excel文件,并运行一个宏 [英] how do i open ALL the excel files one by one and run a macro
问题描述
我需要在VBA中编写一个宏,它将逐个打开给定目录中的每个文件,然后运行一个宏。
到目前为止,我有一些东西像
for i = 1 to number_of_files
打开Dir(C:\yourPath\ *。* ,vbNormal)
call some_macro
关闭文件
下一个我
Dir()
函数与一个适当的过滤器,如c:\folder\\ \\ * .xls
,你开始枚举并获得第一个文件名。之后,反复调用
Dir()$ c $你可以得到所有的 *。xls
文件名,每个调用一个。
通过调用 Workbooks.Open(full_path)
来打开工作簿。这给你一个工作簿
对象,你可以运行一个宏。
.Close()
这个工作簿
对象的方法关闭工作簿。您可以使用 .Close(SaveChanges:= True)
保存更改, .Close(SaveChanges:= False)
放弃更改,或者省略参数以让用户决定。
I need to write a macro in VBA that will open every file in a given directory one by one and run a macro on them.
so far i have something like
for i = 1 to number_of_files
open Dir("C:\yourPath\*.*", vbNormal)
call some_macro
close file
next i
解决方案 By calling the Dir()
function with an appropriate filter, such as "c:\folder\*.xls"
, you start enumeration and get the first file name.
After that, repeatedly calling the Dir()
function without any parameters, you will get all *.xls
file names, one for each call.
You open a workbook by calling Workbooks.Open(full_path)
. This gives you a Workbook
object, against which you can run a macro.
The .Close()
method of this Workbook
object closes the workbook. You can use .Close(SaveChanges:=True)
to save changes, .Close(SaveChanges:=False)
to discard changes, or omit the parameter to have the user decide.
这篇关于我如何打开所有的Excel文件,并运行一个宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!