You are here:

Excel/Macro to open several files in a folder

Advertisement


Question
QUESTION: Hi Tom

I have several files in a folder that I need to open each month, do the same few changes in all of them, then close them.  I have a macro that make the changes, but I would like a macro that would open the first file in the folder, make the changes, close the first file, open the second file, make the changes, close the second file, and so on, until every file in the folder is updated.

Could you tell me how to do this please?

Thanks

Gail

ANSWER:
say in A1:A20 you have entries like

A1: C:\Myfolder\Myfile1.xls
A2: C:\Myfolder\Myfile5.xls
. . .
A20:  C:\MyFolder\MySubfolder3\MyFile2.xls

then you could do

Sub processFolders
Dim cell as Range, r as Range
dim bk as Workbook, s as String
set r = Range("A1:A20")
for each cell in r
 s = cell.text
 workbooks.Open(s)
 set bk = ActiveWorkbook
 ' run you macro that works on the active sheet
 ' example if your exisiting macro is nameed processFile
 ProcessFile
 bk.Close SaveChanges:=True
next
End Sub

You initially said several files in a folder and that is what I wrote to above.

In the end of your question you said all files in a folder.  If so, then


Sub  ProcessFolders()
Dim sPath as String, sName as String
Dim bk as Workbook
sPath = "C:\Myfolder\"
sname = dir(sPath & "*.xls?")
do while sName <> ""
workbooks.Open(sPath & sName)
set bk = ActiveWorkbook
 ' run you macro that works on the active sheet
 ' example if your exisiting macro is nameed processFile
ProcessFile
bk.Close SaveChanges:=True
sname = dir()
Loop
End Sub

would be the general approach


--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Thanks for that Tom, could you tell me how I get this code into my personal macro workbook?  I am not very familiar with VBE, I normally just record macros.  I am using Excel 2007.

Thanks

Gail

Answer
Gail,

do Alt+F11 to get to the visual basic editor

in the left side you should see the project explorer window.  

It will contain a treeview type display with workbook names as the "stems" or "branches" of the tree.  You should have one named  PERSONAL.XLSB

click on that.  If it has not expanded (not have "children" branches below it), then it will have a plus sign where it connects to the tree to the left of the icon.  Click on the plus sign to expand its tree and see its children.  One of the children should be labelled modules.

Click on the plus sign next to Modules if it has no children branches.  When you do that you should see children with module names. Click on one of those or click on the modules branch and go to the menu and go Insert=>Module.

When you have a module displayed in front of you in the bigger part of the window, you can paste the macro there.   

Now go to the file menu in the VBE and click on it to display the dropdown menu.  Click on Save Personal.xlsb

(if that is not offered, go back to the project explorer and make sure you have the personal.xlsb section selected in the project explorer.

Now you can do Alt+F11 to return to excel.

------------------------
An alternative method would be to record a macro and designate it be stored in the personal workbook.  then just take a simple action and end recording (remember the name or give it a special name when prompted where to save it).  Now go to the View menu and Find your macro in the macro dropdown=> view macros.  Click on the macro and choose edit.  Now copy the macro from the posting and paste it over this macro. (you will be in a module in the Personal.xlsb workbook.)   Save the personal.xlsb workbook.


--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.