+ Reply to Thread
Results 1 to 8 of 8

VBA to open, change, save and close all files in DIR

  1. #1
    Registered User
    Join Date
    05-18-2011
    Location
    Hillsboro, OR
    MS-Off Ver
    Excel 2003
    Posts
    6

    VBA to open, change, save and close all files in DIR

    Hello all,

    I have a directory of one sheet workbooks that are all the same template and need to have their sheet renamed from "Sheet1" to whatever the filename is to prepare them for merging into one larger workbook.

    I know the code I need to make the change is 'activeSheet.name = activeWorkbook.name' but since I can't use Application.FileSearch anymore I need to know how to run a module that will easily open the files in the directory one at a time, make the name change, save the file and move on to the next one. I don't need to step in at any point as all the files are already saved with the correct name and they're all getting the same change.

    Since nothing I've found online so far has worked I'm basically starting from scratch with a new module.

    Can someone walk me through the proper way to run the loop to do this?

    Thanks/
    Last edited by jdkoeppen; 05-18-2011 at 04:30 PM.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA to open, change, save and close all files in DIR

    Please Login or Register  to view this content.
    change "E:\OF\' into the directory/folde/map you need.
    change "New name" into the desired sheetname
    Last edited by snb; 05-18-2011 at 03:29 PM.



  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: VBA to open, change, save and close all files in DIR

    Please Login or Register  to view this content.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: VBA to open, change, save and close all files in DIR

    Dang it snb, you're always one step ahead of me! ; )

  5. #5
    Registered User
    Join Date
    05-18-2011
    Location
    Hillsboro, OR
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA to open, change, save and close all files in DIR

    Thanks, snb. I created and saved a new workbook in the directory along with the test files as shown in the following code, ran the macro but nothing happens. No error but no files opened and no changes were made. Am I doing something wrong?


    Please Login or Register  to view this content.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA to open, change, save and close all files in DIR

    @Dave

    Apologies.
    I had an omission in my code: the asterisk.
    I fear you have 1 too: C: needs a backward slash

    Besides it's nice to compare both methods: I try to prevent the screenfilckering by using 'Getobject'.
    I think yours needs still application.screenupdating =False

  7. #7
    Registered User
    Join Date
    05-18-2011
    Location
    Hillsboro, OR
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA to open, change, save and close all files in DIR

    Caught the slash from Dave, added the asterisk and it seems to be working. Now I just need to plug in the rest of my stuff. Thanks guys!

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA to open, change, save and close all files in DIR

    @jdkoeppen

    No you didn't, I did (forgot the asterisk). See the amended code in my first post.

+ 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