+ Reply to Thread
Results 1 to 23 of 23

Make the same change to multiple Excel files

  1. #1
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Make the same change to multiple Excel files

    Hi, I have 900 Excel files that need the same change done to it. I have recorded a macro of me making the change to 1 file, however I don't know how to make the change to all files. These files are inside a drive (D:\ drive, but different folders/sub-folders) and are all named annual.xls. There will be other Excel files there not called annual.xls and those may not be changed. My code for the macro is:

    Please Login or Register  to view this content.
    How can I make this change to all 900 files named annual.xls in the D:\ drive? Any help is appreciated.
    Last edited by fred2028; 04-12-2011 at 09:02 AM.

  2. #2
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    bump a de bump

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Make the same change to multiple Excel files

    You have 900 files called D:\Annual.xls?

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Make the same change to multiple Excel files

    Hello fred2028:

    1)Each file in a directory must have a unique name ... could you please explain what the pattern is for the files .

    2)The code you have included does not refer to any specific page in each workbook... so be aware it will only affect the first active sheet in each workbook .

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Make the same change to multiple Excel files

    Hi Fred,

    Please have a look at this:

    Please Login or Register  to view this content.
    I hope I didn't omit anything important while I was cleaning the code.

    abousetta

  6. #6
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    Quote Originally Posted by jolivanes View Post
    You have 900 files called D:\Annual.xls?
    All the files to be changed are called annual.xls, however they are in different folders and sub-folders. There will be other Excel files as well but they will not be called annual.xls, and they are not to be changed.
    Quote Originally Posted by nimrod View Post
    Hello fred2028:

    1)Each file in a directory must have a unique name ... could you please explain what the pattern is for the files .

    2)The code you have included does not refer to any specific page in each workbook... so be aware it will only affect the first active sheet in each workbook .
    The pattern would be:

    D:\someFolderNameHere\spreads\annual.xls

    where someFolderNameHere can be 900 different things

    Affecting the 1st worksheet is fine since there is only 1 sheet.
    Quote Originally Posted by abousetta View Post
    Hi Fred,

    Please have a look at this:

    Please Login or Register  to view this content.
    I hope I didn't omit anything important while I was cleaning the code.

    abousetta
    I will take a look and get back at you. Thanks very much!
    Last edited by fred2028; 04-12-2011 at 09:02 AM.

  7. #7
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    I've confirmed that the following code works when pasted into a Macro editor for individual files
    Please Login or Register  to view this content.
    however how do I convert this to work on all files called annual.xls in the current folder and its sub-folders? Do I save the code as a .bat file in Windows?
    Last edited by fred2028; 04-12-2011 at 09:57 AM.

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Make the same change to multiple Excel files

    Hi Fred,

    Glad it works. I just need to another another piece of code for it to loop through subfolders. Will hopefully get back to you in a bit.

    abousetta

  9. #9
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    Quote Originally Posted by abousetta View Post
    Hi Fred,

    Glad it works. I just need to another another piece of code for it to loop through subfolders. Will hopefully get back to you in a bit.

    abousetta
    Thanks a lot, I really appreciate you helping. I re-did the macro recording in the post above your previous post.

    Just had another question. How do I save this code so that it affects all folders and sub-folders? Do I save it as a .bat file in Windows or something?

  10. #10
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Make the same change to multiple Excel files

    Hi Fred,

    This updated code should do what you want including looping through subfolders.

    Please Login or Register  to view this content.
    I've added in some descriptions along the way to help you identify areas where you may want to make future modifications.

    On a side note, the macro recorder is an excellent way to record actions like formatting, locking/ unlocking, etc. but also records a lot of unnecessary steps like scrolling or selecting cells. This really slows down your code and for futrue purposes you should learn to clean all this out of your recorded code. As a rule of thumb, you never need to scroll and almost never need to select ranges.

    Good luck.

    abousetta

  11. #11
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    Quote Originally Posted by abousetta View Post
    Hi Fred,

    This updated code should do what you want including looping through subfolders.

    Please Login or Register  to view this content.
    I've added in some descriptions along the way to help you identify areas where you may want to make future modifications.

    On a side note, the macro recorder is an excellent way to record actions like formatting, locking/ unlocking, etc. but also records a lot of unnecessary steps like scrolling or selecting cells. This really slows down your code and for futrue purposes you should learn to clean all this out of your recorded code. As a rule of thumb, you never need to scroll and almost never need to select ranges.

    Good luck.

    abousetta
    Thank you very much abousetta, I will try out the code as soon as I'm done this other project. I'll also look into removing the code for scrolling and selecting. I really appreciate your help!

  12. #12
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    Quote Originally Posted by abousetta View Post
    Hi Fred,

    This updated code should do what you want including looping through subfolders.

    Please Login or Register  to view this content.
    I've added in some descriptions along the way to help you identify areas where you may want to make future modifications.

    On a side note, the macro recorder is an excellent way to record actions like formatting, locking/ unlocking, etc. but also records a lot of unnecessary steps like scrolling or selecting cells. This really slows down your code and for futrue purposes you should learn to clean all this out of your recorded code. As a rule of thumb, you never need to scroll and almost never need to select ranges.

    Good luck.

    abousetta
    Hey abousetta, thanks for your help. It works fine, with just 2 minor issues

    1) I can unprotect the worksheets without a password ... Is there a way to prompt for a password when trying to unprotect?

    2) Cells F1:F5 do not seem to be changing colours for some reason, even though I see it in the code

  13. #13
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Make the same change to multiple Excel files

    Hi Fred,

    The first question is easy. Just remove this part of the code that passes the password to the excel sheets:

    Please Login or Register  to view this content.
    Of course now you will have to provide a password each time a file is protected.

    Second part, I tried it and it seems to change the font color. Did you want it to change the cell fill in color instead? In other words, if you type something in these cells, you find that the font is Arial, 9 pts, with a light blue font color but the cell itself does not have any shading.

    abousetta

    P.S. you don't have to quote the whole post. If you need to quote a part of it then that is fine, but quoting the whole post especially if it contains code makes it difficult to find your post.

  14. #14
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    For the first question I meant after the script is run, I can unprotect the worksheet without using a password even though there is a line that says .Protect "123"

    I just want to change the font colour in the cell, not the fill colour. I am using Excel 2010 on Windows XP x86, is that an issue?

    Also, how are you supposed to run these scripts? Currently, I am running them inside a macro (Developer tab > Macros) but I don't know if that is the right way.

    Thanks!

  15. #15
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Make the same change to multiple Excel files

    Hi Fred,

    I am a little confused so please bear with me. The macro will open each file (and if the file is password protected then it unprotects it using the password "123") makes the modifications and then password protects the file (using the password "123") before saving and closing the file. If you come to open the file through Excel or by clicking on it after the code has run, it should ask for a password. This is what happens on my computer (Windows 7, Office 2010) and should happen the same way on your system as well.

    As for the second part, the font color is changes. If you write something in the cells then it should appear a shade of blue. Is something different happening on your system. If so, please upload a copy of one of the files that has already been modified by the macro.

    Finally, there are many ways to run a macro but the most common three ways off the top of my head are (1) the way you described (or Alt+F8), putting a button (or other shape object) in the excel sheet and clicking on it, or by assigning a combination of Ctrl+Alt+ a letter. They will all acomplish the same task.

    abousetta

  16. #16
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    Quote Originally Posted by abousetta View Post
    Hi Fred,

    I am a little confused so please bear with me. The macro will open each file (and if the file is password protected then it unprotects it using the password "123") makes the modifications and then password protects the file (using the password "123") before saving and closing the file. If you come to open the file through Excel or by clicking on it after the code has run, it should ask for a password. This is what happens on my computer (Windows 7, Office 2010) and should happen the same way on your system as well.
    Hmmm I will try this on my Windows 7 laptop at home today and see what happens.

    As for the second part, the font color is changes. If you write something in the cells then it should appear a shade of blue. Is something different happening on your system. If so, please upload a copy of one of the files that has already been modified by the macro.
    On my system, the font colour in cells F1:F5 remain black. There is already text in the cells, and their colours have not changed. I have attached a file with the macro in it. The cells, F1:F5, are black.
    Finally, there are many ways to run a macro but the most common three ways off the top of my head are (1) the way you described (or Alt+F8), putting a button (or other shape object) in the excel sheet and clicking on it, or by assigning a combination of Ctrl+Alt+ a letter. They will all acomplish the same task.

    abousetta
    OK, so just to be clear, running this macro the way I described will modify each file named annual.xls in this folder and its subfolders even though it's running from within an Excel file?
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    OK I just tried again, and the file annual.xls is protected after I run the code, however if I close then reopen the file, I can edit all cells (even though it is Protected). I can unprotect the sheets with no password ...

  18. #18
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Make the same change to multiple Excel files

    The other macros in your sheet are unprotecting. I will track it down at lunch and give you my advice on how to resolve this.

    abousetta

  19. #19
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    Quote Originally Posted by abousetta View Post
    The other macros in your sheet are unprotecting. I will track it down at lunch and give you my advice on how to resolve this.

    abousetta
    Thank you very much!

  20. #20
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Make the same change to multiple Excel files

    Hi Fred,

    OK, so after looking at your code, it seems that you have 34 instances of unprotecting and protecting. I have globally added the password "123" to all of them. Therefore when the code runs into one of the unprotect arguments then it will unprotect using the password "123" and same thing for the password argument.

    This should resolve the issue with the protection.

    Are you still having problems with the font color?

    abousetta
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    Thanks I looked through the other macros and it seems to be the case. However, I am unclear as to how the other macros can unprotect the worksheet without the password even though the macro we wrote protected it with the password "123". Could you shed some light on this?

    Also, is it possible for me to add the password "123" to each macro in each worksheet recursively, in the same fashion I am doing now for the formatting changes? Thanks.

  22. #22
    Registered User
    Join Date
    03-08-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Make the same change to multiple Excel files

    After the aforementioned problems, I'm wondering if it's possible to do the changes to the Excel files via a .NET Windows application? If so, what are some topics I should read into, for things like opening Excel files, manipulating them, etc.?

  23. #23
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Make the same change to multiple Excel files

    Hi Fred,

    I really don't know why its allowing it to unprotect without a password. It is interesting because I thought it needed a password to unprotect a worksheet, even via vba but I guess I was wrong. Maybe the more knowledgeable folks here can shed some light on this subject.

    abousetta

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1