Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Making Changes in a Group of Workbooks.

Making Changes in a Group of Workbooks

by Allen Wyatt
(last updated December 20, 2014)

3

Over time, it is very easy to create and collect a huge number of Excel workbooks. Suppose that you had a whole bunch of workbooks in which you needed to make the same change. For instance, you might need to change the value stored in cell A10 of each of the worksheets in each of the workbooks.

If you had only a few workbooks to change, the task is pretty easy: Load each workbook and, in turn, make the change to each of them. If you have a couple hundred workbooks in which the change needs to be made, then the task becomes more formidable.

If you anticipate only needing to do this task once, then the easiest solution is to create a text file that contains the path and filename of each of the workbooks, one workbook per line. For instance, you might end up with a file that had entries such as this:

c:\myfiles\first workbook.xls
c:\myfiles\second workbook.xls
c:\myfiles\third workbook.xls

The file could have as many lines in it as necessary; it doesn't really matter. The important thing is that each line be a valid path and file name, and that there be no blank lines in the file.

You could most easily create such a file by displaying a command-prompt window, navigating to the directory containing the workbooks, and issuing the following command:

dir /b > myfilelist.txt

Each file in the directory ends up in the myfilelist.txt file. You will need to load the text file into a text editor and check it out so you can delete extraneous entries. (For instance, myfilelist.txt will end up in the listing.) You will also need to add the path name to the beginning of each line in the file.

Once the file is complete, you can start Excel and use a macro to read the text file, load each workbook listed in the text file, step through each worksheet in that workbook, make the appropriate change, and save the workbook. The following macro will perform these tasks nicely.

Sub ChangeFiles1()
    Dim sFilename As String
    Dim wks As Worksheet

    Open "c:\myfiles\myfilelist.txt" For Input As #1
    Do While Not EOF(1)
        Input #1, sFilename  ' Get workbook path and name
        Workbooks.Open sFilename

        With ActiveWorkbook
            For Each wks In .Worksheets
                ' Specify the change to make
                wks.Range("A1").Value = "A1 Changed"
            Next
        End With

        ActiveWorkbook.Close SaveChanges:=True
    Loop
    Close #1
End Sub

While this approach works great if you only have to process a single batch of workbook files, it can be made much more flexible if you anticipate needing to make such changes in the future. The biggest hassle, of course, is putting together the myfilelist.txt file each time you want to process a batch of files. Flexibility is added if the macro could simply use a directory and then load each workbook from that directory.

Sub ChangeFiles2()
    Dim MyPath As String
    Dim MyFile As String
    Dim dirName As String
    Dim wks As Worksheet

    ' Change directory path as desired
    dirName = "c:\myfiles\"

    MyPath = dirName & "*.xls"
    MyFile = Dir(MyPath)
    If MyFile > "" Then MyFile = dirName & MyFile

    Do While MyFile <> ""
        If Len(MyFile) = 0 Then Exit Do 

        Workbooks.Open MyFile

        With ActiveWorkbook
            For Each wks In .Worksheets
                ' Specify the change to make
                wks.Range("A1").Value = "A1 Changed"
            Next
        End With

        ActiveWorkbook.Close SaveChanges:=True

        MyFile = Dir
        If MyFile > "" Then MyFile = dirName & MyFile
    Loop
End Sub 

This macro uses whatever directory you specify for the dirName variable. Any workbook file (ending with the .Xls extension) is loaded and processed.

Another approach is to have the macro ask the user which directory should be processed. You ca use the standard Excel File dialog box to do this, in the manner shown in the following macro.

Public Sub ChangeFiles3()
    Dim MyPath As String
    Dim MyFile As String
    Dim dirName As String

    With Application.FileDialog(msoFileDialogFolderPicker)
        ' Optional: set folder to start in
        .InitialFileName = "C:\Excel\"
        .Title = "Select the folder to process"
        If .Show = True Then
            dirName = .SelectedItems(1)
        End If
    End With

    MyPath = dirName & "\*.xls"
    myFile = Dir(MyPath)
    If MyFile > "" Then MyFile = dirName & MyFile

    Do While MyFile <> ""
        If Len(MyFile) = 0 Then Exit Do 

        Workbooks.Open MyFile

        With ActiveWorkbook
            For Each wks In .Worksheets
                ' Specify the change to make
                wks.Range("A1").Value = "A1 Changed"
            Next
        End With

        ActiveWorkbook.Close SaveChanges:=True

        MyFile = Dir
        If MyFile > "" Then MyFile = dirName & MyFile
    Loop
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3176) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Making Changes in a Group of Workbooks.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Easily Adding Blank Rows

Want to add a bunch of blank rows to a your data and have those rows interspersed among your existing rows? Here's a ...

Discover More

Running a Procedure when a Workbook is Opened

Ever want to have Excel run a procedure whenever you open a workbook? It's not as difficult as you might think. Here's how.

Discover More

Conditionally Formatting Non-Integers

The conditional formatting capabilities of Excel are very helpful when you want to call attention to different values ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

File Formats that Include Field Formats

If you import data into Excel that is created by other programs, you know that it can be bothersome to get your data ...

Discover More

Importing Many Files Into Excel

Importing a single file is easy. Importing a whole slew of files can be much more of a challenge.

Discover More

Getting Rid of Extra Quote Marks in Exported Text Files

If you don't like the way that Excel exports information you intend to use with other programs, then your best bet is to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 8?

2019-07-19 14:30:32

Joshua Gottesman

Allen,

Thanks for the code above. I am using a ribbon version of Excel, and I think the principles will still apply. However, my situation is a little more complex, so I was hoping you might have some insight. Here's the basic situation. Each week I have to track the spending of 4 different checking accounts. Right now I have each one in a separate file. Each file has the same structure, with a Summary tab, and then tabs like Week Ended 070719 and Week Ended 071419. I set up a master file with the column headers for each type of expense and I have the Summary sheet in each workbook linked to the master titles to pick up the column headers. The other tabs then link to the summary sheet.

Ideally, what I'd like to do when I need to add a column is this. Note that I want to keep the columns in alphabetical order so its not as simple as always adding one to the right.

1. Click a button in the Master Titles file. This prompts to select a column (or ask for a column, if that's easier) where I want to insert a column.
2. It adds the column to the Master Titles file.
3. It opens each of the other files (there are 4 of them) and adds the same column to those files and copies the formula to pick up the column name from Master Titles and copies the formula on the sub-tabs to pick up the new column data from the Summary tab.
4. Save all the open files.

The other alternative is to put everything in one workbook, however that would involve multiple tabs with the same name, which wouldn't work (I could change that naming scheme if that's easier). It also gives me 1 sprawling workbook instead of multiple smaller ones. I could also put all of the data in one workbook with a separate section for each account on each tab. This might be simplest, however its not as elegant as I'd like.

Any suggestions would be appreciated.

Thanks,

Joshua


2014-12-22 11:52:13

Scott Renz

To mininize the number of extra file names in the output, you can instead of starting one level up, start on the targeted folder and use "." (the current folder) as the target.

dir /s /b . >myfilelist.txt

If your targeted folder has no child folders, then it would be the only one that gets hits. Otherwise, you would have to eliminate those child folder files from your list after the fact.


2014-12-22 11:44:15

Scott Renz

If you start your command line dir command in the directory one level above your target folder and add /s to the dir command then /b will also add the full path drive and folder name in front of the file name.

dir /s /b > myfilelist.txt

You can then retain only the ones with the drive and folder path name that you are targeting.


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.