+ Reply to Thread
Results 1 to 26 of 26

Replace a string in over 250 excel files

  1. #1
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Replace a string in over 250 excel files

    Sup,
    I've been given the assignment to replace an old company name with the new one in an excessive amount of excel files. All the excel files seem to be some kinds of forms with fancy buttons and such. I've been trying to research this quite a bit but only found some shady programs which I'm not installing on company computers so I decided to write a batch script to try and accomplish this, if you have any other ways to do this do let me know. So far I've only got a basic skeleton of the batch script, like looping through all the excel files in a directory and then loop through all the worksheets within the file. Here's how it looks

    Please Login or Register  to view this content.
    I just want some confirmation that this would actually work and how to replace all occurrences of a string in a workspace.

    Thank you!
    Sincerely, Glitches.

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Replace a string in over 250 excel files

    I don't know whether your code will work, but this seems to.

    In Excel VBE add a Module.

    (semi-tested)
    Please Login or Register  to view this content.
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    Wow! Thanks,
    but there's a couple of other things. The files are password protected so you have to enter a password each time you change a sheet (I have the password, ofc), is this easy to implement?
    And how would I run this? Do I just install Excel VBE and add this as a module and run it?

    Thanks,
    Sincerely, Glitches.
    Last edited by Glitches; 05-28-2014 at 10:42 AM.

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Replace a string in over 250 excel files

    Hopefully, the password is the same across all worksheets!

    For Workbook, just in case.
    Please Login or Register  to view this content.
    For each WS

    Here's whole loop
    Please Login or Register  to view this content.
    Last edited by Tinbendr; 05-28-2014 at 10:44 AM.

  5. #5
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    Thank you so much!
    But how do I run this? do I just install Excel VBE and add it as a module and run it there?

    Sincerely, Glitches.

  6. #6
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Replace a string in over 250 excel files

    From Excel. Press Alt-F11. Insert->module. Copy code and paste it in. After correcting path and our revisions, F5 to run the code.

    Sent from my phone using Tapatalk.

  7. #7
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    Thank you, I managed to get it running, and I've got one more question, Does it work recursively, like does it go to into all directories?
    This is currently going through 1,2 GB of excel files, I'm just gonna leave it over a couple of days to see if it can't manage to finish all of the files, cuz this is quite a lot massive files.
    It's been 15 minutes and it's still on the first file, so I'm not sure if it's working, but whatever.
    I'll report back and tell you how everything went.

    Sincerely, Glitches.

  8. #8
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Replace a string in over 250 excel files

    I would test it on one subdirectory first. It shouldn't take very long . you might have to stop it. Then use F8 and step through the code to see what's going on. Then when your comfortable with it, let it run unrestricted. It may be waiting for a response.

    Sent from my phone using Tapatalk.

  9. #9
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Replace a string in over 250 excel files

    If it's hung up on a display alert (like, Are you sure...?) you can turn off display alerts.

    Application.DisplayAlerts = False.

    But this is like turning error checking off, and might hide problems. You can run it over just a few lines of code, the, turn it back on, until you've narrowed down the place you need to skip.

    No, this isn't recursive. I musta grab the wrong section. I'll see if I can't find the recursive version.

  10. #10
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    Well, I won't go back into work until Friday, and I just left it running on my machine so I'll just have to check on it then and see if it's still on the first file.
    I'm not sure if I can remote desktop into the machine from home... I'll have to check on that.
    The recursive script would be great, otherwise I'll just put all the files in a single directory.

    Thank you, you've been extremely helpful!
    Sincerely, Glitches.

  11. #11
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Replace a string in over 250 excel files

    OK, this seems to work. You'll have to add password code back in.
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    Thanks!
    I'll check it out in the morning, did you test it out recursively?

    Sincerely, Glitches.

  13. #13
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Replace a string in over 250 excel files

    It seemed to iterate and record the sub folders correctly.
    If you're not aware, the code writes to the sheet to give some feedback to what it's done already.

    Sent from my phone using Tapatalk.

  14. #14
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    Ok, I've got it running but it's kind of strange, I had to change (ObjFile.Name Like "*.xls?") to (ObjFile.Name Like "*.xls"), otherwise it would skip all of the files (except one for some reason). It stops on the first file on (Loop While Not C Is Nothing And C.Address <> FirstAddress) with a run-time error: 91 "Object or variable not set". I can't see what's wrong

    Sincerely, Glitches.

  15. #15
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Replace a string in over 250 excel files

    Another way would be to put all *.xls files into an array using the Dos Dir command and then loop through the array and use the Findnext code to 'Find and Replace" the text..

    Here's a snippet.. if you need.. I will make a solution tomorrow (it's way late here)..

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    I would really appreciate that, but would it work recursively and with multiple sheets, though?

    Sincerely, Glitches.

  17. #17
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Replace a string in over 250 excel files

    Hey..

    Yeap. the /s part of the Dir command makes it include Sub Folders..

    Multiple sheets... just loop through each worksheet in each Workbook..

    I'll post something tomorrow...

  18. #18
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Replace a string in over 250 excel files

    Hmmm......

    Try this.
    Please Login or Register  to view this content.

  19. #19
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Replace a string in over 250 excel files

    This should do it too..


    Change OldComanyName, NewCompanyName and the filepath to suit..

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    Thanks, I'll check it out on monday!
    But all my files are password protected, would that work with this?

    Sincerely, Glitches.

  21. #21
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Replace a string in over 250 excel files

    See Post #4 by Tindendr...

    If you can't implement something similar.. post back and let me know if just the Workbook is password protected, the Worksheets or both..

  22. #22
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    I'll see if I can implement it myself.

    Thanks
    Sincerely, Glitches.

  23. #23
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    It looks a little bit more promising, but I'm getting an error on the first file it says "Excel has detected that this file contains labels in formulas. These are no longer supported in Excel and will be replace with cell references. Your formulas will continue to work correctly. You cannot undo this change. Do you want to continue?", When I press 'Yes' I can't see any changes in the file. It might just be the files and not anything in the script, but how would I make the script just ignore it and say 'Yes'?
    Last edited by Glitches; 06-02-2014 at 04:41 AM.

  24. #24
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    It seems to stop on the first file as well with a run-time error '432' "File name or class name not found during Automation operation" on the line "With GetObject(x(i))"

  25. #25
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    I just found out that some files are password protected and others are protected but with no password, so when the script runs (WS.Unprotect "MyPassword") it says it's incorrect on the files with no password. but they still need to be unprotected just with (WS.Unprotect "") I think. Is this possible?

    Thanks
    Sincerely, Glitches.

  26. #26
    Registered User
    Join Date
    05-27-2014
    Posts
    16

    Re: Replace a string in over 250 excel files

    Wait, I just realized some of the files aren't even protected so they are giving me an error at (WS.Unprotect "Pass") It would be nicer if it would be possible to check if the sheet is protected and then try a password and if that doesn't work try no password

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replace string crashes Excel
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-21-2014, 06:13 AM
  2. the ' symbol ends the SQL string, how do you replace it Excel
    By Valori in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-02-2006, 09:50 PM
  3. Excel - Find & Replace text in a string
    By bklim in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-14-2005, 02:37 AM
  4. [SOLVED] How do I replace * as a character in a string in Excel?
    By nicolegt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-21-2005, 07:06 AM

Tags for this Thread

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