Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i open ALL the excel files one by one and run a macro

Tags:

file-io

vba

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
like image 975
Alex Gordon Avatar asked Jul 30 '10 20:07

Alex Gordon


1 Answers

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.

like image 72
GSerg Avatar answered Sep 19 '22 01:09

GSerg