+ Reply to Thread
Results 1 to 14 of 14

Loop sheets in more than one workbook

  1. #1
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Loop sheets in more than one workbook

    Hi
    I have a small macro that searches the sheets in a workbook and sends the info (if qualifies) to a new workbook before saving that workbook using a name date time format for records.

    I woud like this macro to be able to repeat action in 8 more selected workbooks in a folder.
    Question - can I name the workbooks I want to search - and - can I search all 9 workbooks before the data sheet saves and names itself, limiting access.

    Code and sample attached
    Please Login or Register  to view this content.
    Any assistance appreciated. Rgds Nigel
    Attached Files Attached Files
    Last edited by nigelog; 02-02-2009 at 10:44 AM. Reason: Rephrase of question

  2. #2
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: Loop sheets in more than one workbook

    Hi
    place the 8 files in a folder along with nigel.xls (attached ) and run the macro. It will list 8 files in col A, opens each, runs macro2, saves them,closes it and next file opens
    Ravi
    Attached Files Attached Files

  3. #3
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    Quote Originally Posted by ravishankar View Post
    Hi
    place the 8 files in a folder along with nigel.xls (attached ) and run the macro. It will list 8 files in col A, opens each, runs macro2, saves them,closes it and next file opens
    Ravi
    Hi Ravishankar

    I will have a look at this. If after running the macro I can then return the files to their original folder (there is no chance I can permanently move them or delete surrounding files)and specify the order they are opened then it will be what I am looking for.

    Let you know

  4. #4
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    Sorry Rav

    first part of macro collected the filenames and listed them in a as stated. Macro then called file Chipliner - OK

    but before collecting data as per Macro(2) it copied whole of workbook Chipliner to Fault Report 27012009 5:12 TR136 (dont know where TR136 came from it is the last sheet in the chipliner workbook)

    It also had saved the workbook which I would have thought data from all selected files would need to have been colllected before save invoked. I have attached fault file created.

    I will have a look to see what he cause is. Also the fault file, although not in the folder as directed to C:\, appears in the file list at the top.
    File would'nt attach. Going to look at re ordering file list and removing fault file from top
    Last edited by nigelog; 01-28-2009 at 01:47 PM.

  5. #5
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    Here is the code uploaded by Ravashanker. It is going in the right direction and does collect the list of files in the folder.
    However when the macro nigel() is run it goes through the first filemane in the list -opens - closes - but then it adds the fault file which is created by macro2() to A1 and "c:\" to B1.

    It then rolls to next file in list "EJECTORS" but gives an error filename not found. I assume that it is now searching in C:\ instead of the folder.

    Ravs code including my macro 2()
    Attached file in which macro works in one workbook
    Please Login or Register  to view this content.
    There are only 8 workbooks in a folder containing 20 which are to be involved in this loop. If macro can thereby operate from a fixed list in seperate location it would be preferable.

    I am struggling with this and any help appreciated.
    Attached Files Attached Files

  6. #6
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    Try as I might I cannot get pass the error recorded when this line is highlighted(underlined in code).

    Should the file nigel.xls be outside of the folder, and the folder targeted, in question before activating macro

    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    By changing the destination of my created faults file in macro 2 to the same folder as all of the source workbooks has now allowed the macro to continue looping through the workbooks in the folder.

    However, as a result it is copying the entire contents of each workbook to the faults file and not the result of macro ran within the file.

    I have searched for two days on this and other forums and I am surprised I cannot find a way to even stipulate a list a workbooks for the macro to work within.

    I am going to drop Ravashankers solution entirely at this point unless I can find it can be adapted at a later stage.

    At this stage I feel like just sticking a button in each workbook.

    Any fresh input appreciated

  8. #8
    Registered User
    Join Date
    05-02-2007
    Location
    US
    MS-Off Ver
    2000 @work, 2003 @home
    Posts
    23

    Re: Loop sheets in more than one workbook

    I tried the code that was provided by ravishankar and it works pretty good. Notice:
    place the 8 files in a folder along with nigel.xls (attached ) and run the macro.
    i believe the request here is to just only have the 8 files and plus nigel workbook in a folder by itself. This way, you always have an uptodate list shown.
    What I see you could do? Here is something you could try, if you don't have a problem with names of the workbook not updating continuously.
    I stepped through the macro to get all of the names of the workbooks into Column A, stopped the macro. Then I commented out the part (for safekeeping in the future) that will keep making the list:
    Please Login or Register  to view this content.
    Then I removed any unwanted files from the list generated, made sure to keep the cell A1 and B1 intact as well. Then stepped through the macro again to make sure it was opening the desired files. When I was satisfied with the results, I just ran the macro from there.

    Of course, make sure you have a back up of any copy that you are doing so that you can at least backtrack. And if the file names do update and you don't keep the list current, then you will probably have a fail.
    At least, that is what I would do.
    Hope this helps
    Thanks,
    David
    yes, there is only one c

  9. #9
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    Hi David
    thanks for the response. Commenting out the section as you directed has indeed helped matters as the list was updating itself and including the destination file of macro2() and of course nigel.xls.

    I have corrected this and can now loop through the appropriate files.

    Problem now is the calling of macro2(). Macro2() now is copying the entirety of the open workbook to the destination file and not the "results" of the macro.

    It also when it opens the second workbook overwrites the first workbooks results with the second in the destination file. It would need to add these results rather than overwrite so the destination workbook would have to remain open.

    Macro2() when run in any of the 8 files correctly gives the sorted results so in itself is not at fault.

    I attach the workbook nigel.xls and one of the workbooks in question.

    Any help appreciated. Rgds Nigel

    I have just noticed (but do not know how to correct) that macro2() when ran within Macroom*.xls works correctly when macro is invoked from within the file. If however the workbook nigel1.xls is open and the macro2() invoked from there, it gives the incorrect results.
    Attached Files Attached Files

  10. #10
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    Bump - no answer as yet. Rgds Nigel

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Loop sheets in more than one workbook

    Try this code,

    In the file Nigel1.xls you need to change the contents of A1 so that it contains the path where the files are located.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  12. #12
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    Hi Andy

    mush appreciated. Code has worked a treat. I have implemented the code now into the appropriate workbook for launch and all works fine.

    I will list as solved but I have 1 question. The target file is created and saved but it would be preferable if once the macro is activated that this saved fault file is left as the active workbook ready for the operator to print.

    What section of code would I ammend for this to happen.

    Again thanks to all who contributed to this answer.

    Rgds Nigel

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Loop sheets in more than one workbook

    If you want the final left open then comment out the Close method.

    Please Login or Register  to view this content.

  14. #14
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Loop sheets in more than one workbook

    Quote Originally Posted by Andy Pope View Post
    If you want the final left open then comment out the Close method.

    Please Login or Register  to view this content.

    Perfect, many thanks. Rgds Nigel

+ 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