+ Reply to Thread
Results 1 to 8 of 8

Loop through sheets in a workbook

  1. #1
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Loop through sheets in a workbook

    I have the following code that assigns cell values to an array. Then it searches for the array values on another sheet and if it finds them, it copies the value of two cells to that sheet. The code works well on the selected sheet, one sheet at a time. I have 27 sheets in the workbook and I would like to run the code on all the sheets except the sheets named "1Summary", "2Blank" and "SheetNames" (This last sheet is hidden.) I'm having trouble finding the loop to make it work properly. In the sample file I posted, if we use the sheet named "AD-HIGHLAND", I would like to search column A for Last Name and column B for First Name. In this case it would find "Balkaran" and "Anita". Then I would like to find "Balkaran" and "Anita" in the sheet "1Summary" and from "AD-HIGHLAND", copy G17 to D7 in sheet "1Summary" and H17 to E7 in sheet "1Summary". It should then continue to search for any more names in Columns A and B in "AD-HIGHLAND". It would find "Kitney" in column A and "Sean" in column B and from "AD-HIGHLAND", copy G33 to D28 in sheet "1Summary" and H33 to E28 in sheet "1Summary". This process would continue until there are no more names in columns A and B. Then it would repeat the whole process for the remaining sheets with the exception of the three sheets "1Summary", "2Blank" and "SheetNames". Any suggestions would be greatly appreciated.

    Please Login or Register  to view this content.
    This is a link to another post:
    http://www.ozgrid.com/forum/showthre...472#post631472
    Attached Files Attached Files
    Last edited by Mumps1; 10-11-2012 at 03:19 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Loop through sheets in a workbook

    Hi

    Try this as the outer loop
    Please Login or Register  to view this content.
    You could either make this a main sub and have it call your existing sub, or make sure you put
    Please Login or Register  to view this content.
    as the first line of the inner processing.

    rylo

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Loop through sheets in a workbook

    Hello rylo. Thank you so much for your quick response. I tried your suggestion and it worked for the first sheet only without looping to the remaining sheets. I believe that this happens because the line:
    Please Login or Register  to view this content.
    exits the sub when the variable x is empty. It becomes empty when a name is not found in the active sheet. If I remove this line of code, I get a 'Subscript out of range' error on this line:
    Please Login or Register  to view this content.
    I believe that this happens because 'MyArray(5)' and 'MyArray(6)' are empty at this point. Somehow, I need to have the code go the next sheet after all the names are found and x and 'MyArray(5)' and 'MyArray(6)' become empty. I can't figure out a solution to this problem.

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Loop through sheets in a workbook

    Hi

    I put the following and your code in a general module in the example workbook, and it seemed to run and bring back results. Where is it not working correctly?

    Please Login or Register  to view this content.
    rylo

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Loop through sheets in a workbook

    Hi rylo. When I try it, it works for the first sheet only. If I disable the line:
    Please Login or Register  to view this content.
    I get a 'Subscript out of range' error. If I change the above line of code to
    Please Login or Register  to view this content.
    it puts zeros ($0.00) in all the cells in columns D and E in Sheets("1Summary").

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Loop through sheets in a workbook

    Hi

    Are you running it on the 2 example files?

    rylo

  7. #7
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Loop through sheets in a workbook

    Hello rylo. I am running it on the file I attached to my original posting.

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: Loop through sheets in a workbook

    I have tried a different approach and it works.

    HTML Code: 

+ 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