+ Reply to Thread
Results 1 to 19 of 19

Loop not running on all worksheets

  1. #1
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Loop not running on all worksheets

    Hi all,
    I have tried compiling separate macros I've used, but need one of them to loop through each worksheet (it previously only ran on a single worksheet at a time).

    The goal is to have the first part of the macro insert the worksheet name in the blank cells of Column C, and to repeat through all of the worksheets. Once that is done, the macro creates a new worksheet called "Consolidated" and then pulls the data from all other worksheets and compiles the data.

    My problem is that when I run the macro, only the active worksheet is inserting the worksheet name in the appropriate cells. The consolidation then runs, but it is missing data that wasn't created by the first part of the macro.

    Your help would be appreciated.

    Please Login or Register  to view this content.
    Last edited by usmc1sok; 09-09-2013 at 12:45 PM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Loop not running on all worksheets

    your code seems correct, attach please a sample file for testing
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    I attached a sample file. Adjusted code is below to change the offset from (0,-7) to (0,-2). I tested with a cell on Sheet "1125" activated, and the sheet name "1125" was inserted in the appropriate cells in Column C, and the macro continued to create the "Consolidated" sheet and consolidate the data onto it. However, because only Sheet "1125" had values in Column C, all the other cells in Column C on Sheet "Consolidated" are blank (no data on source sheet, therefore no data on consolidation sheet).

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

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop not running on all worksheets

    In the first loop you aren't using the loop variable, ws, to refer to the worksheets.

    In the second you are.

    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    The new code returned a couple of errors. The first said "Excel cannot complete this task with available resources. Choose less data or close other applications." It also gave Run-time error 1004 - "Copy method of range class failed." It was caused by the last line of code below.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop not running on all worksheets

    Oops.

    That should be this.
    Please Login or Register  to view this content.
    Actually that's the only portion I had trouble 'converting'.

    I mean, what is happening here?
    Please Login or Register  to view this content.
    I think you are trying to insert formulas but it's not really clear where you want to insert them.

    The way I interpreted it was that you wanted the formulas in the current region of C2, ie the region that contains C2.

    Is that right?

  7. #7
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    What the code was doing was selecting C2 on the active sheet, then entering the formula to insert the sheet name into C2. After that, it copied C2, went to the last cell (Ctrl+End), then offset to end up at the bottom of Column C. *This part was done so that only the necessary rows in Column C would have the sheet name inserted as opposed to the entire column.* Once the last applicable cell in Column C was activated, the code then selected up to C2 and pasted values. It then converted the selection to numbers instead of text.


    The code above pasted the sheet name all over the place and all the way down to the bottom of Column C (this is what the original code was avoiding), as well as other columns. It also gave an error - Run-time Error 1004 - Application-defined or object-defined error on the last line of code above.
    Last edited by usmc1sok; 09-09-2013 at 04:09 PM.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop not running on all worksheets

    Do you just want to put a formula in column C on each sheet, going down to the last row based on one of the other columns?
    Please Login or Register  to view this content.
    I've just tried the above code in the uploaded workbook and it seems to put the name of the sheet in column C.

    If that's what you want to do.
    Please Login or Register  to view this content.
    Last edited by Norie; 09-09-2013 at 04:27 PM.

  9. #9
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    When I paste the code in , I get a compile error: syntax error on the second line of the code above.

    The pasting of values into Column C is not based off of a specific column. Each sheet has a header in Column C. The sheet name needs to be pasted below that, down to the last row. However, the last row may be determined by any of the other columns (i.e. the last row may only have data in Column A, or only in Column B, or in Columns A and D, etc.).

    I think we're straying off topic. The code I listed in my original post to paste the correct values in the correct place was working just fine, as was the rest of the code. The only problem I am having is getting the first part of the code to loop so that it runs on each worksheet (inserting the sheet names into Column C). It will only run on the active sheet. I can make it start at the first sheet, but then it will only run on that sheet. It does not loop and continue to the rest of the sheets in the workbook before it compiles all the data onto the "Consolidated" sheet.

    I'm also finding errors with your original code further down (Sheets(2).Range("A1")...) once I replace my original code to paste in the sheet names. As I said, my original code works fine for everything except getting the first part to loop. I think the problem is with my For Each loop. If I remove the End If, I get a Compile Error: Next without For. I'm not sure how to fix it, because the code starts with For Each. Is it possibly a nesting issue? Use the attached file and the adjusted code below:

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

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop not running on all worksheets

    This code has no syntax errors, it puts the sheet name in column C from row 2 down to the last row of data in column B - you could use anuy of the other columns, they all have the same no of rows.

    There's was a typo in what I posted earlier, CurrentRange should be CurrentRegion.
    Please Login or Register  to view this content.
    The resultant Consolidated has the data, approx 507 rows in total, from all the other sheets listed sequentially.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop not running on all worksheets

    Forgot to upload file.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    OK, so what I was trying to say about there not always being another specific column to reference as far as pasting the sheet name, is a very important piece of information. When I took out pieces of data, the code did not work correctly, because it's trying to reference a specific column (still doesn't work if you change the column from B to any of the others). I've attached a better sample file, one in which the last cell (Ctrl+End) does not match the row in which Column B (or any other reference column) has data. The code I previously posted goes to the last row with data (regardless of column).

    The looping part worked, but once I changed the data (see attached file), the code did not work properly.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop not running on all worksheets

    Does the updated workbook I uploaded work?

    How did it not work when you took out data?

    Also, is there any column that could always be used to find the last row of data?

  14. #14
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    It works, but not when I start taking data out of the last row (see the V3 file I attached). It is unlikely that, in reality, the last row on every sheet will have data in every column, so no, there is no column to reference (hence my original code). That's why the V3 file is a better test file. I'm sure there's a shorter way to code what I did, but I'm not sure that's the issue. The issue is that the loop around that part of the code isn't working properly.

  15. #15
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    Bump - No answer in 3 days. I think the person who was working on this hit a roadblock.

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop not running on all worksheets

    Eh, not quite.

    I was kind of hoping for a sample workbook where some of the data had been removed.

    I actually thought you had as you mentioned a V3 file, bit I can't seem to find it in the thread.

  17. #17
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    Consolidation Macro Test V3.xlsmSorry - not sure what happened. Should be there now.

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop not running on all worksheets

    Still no info on what doesn't work when data has been removed but give this a try.
    Please Login or Register  to view this content.
    Last edited by Norie; 09-12-2013 at 02:33 PM.

  19. #19
    Registered User
    Join Date
    07-23-2013
    Location
    DE
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Loop not running on all worksheets

    Works like a dream. Thanks.

+ 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. Call Macros before Next loop (loop creates new worksheets)
    By mantooth29 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2012, 05:47 PM
  2. Running One Macro in Multiple Worksheets & Loop
    By mjali001 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-10-2011, 04:57 PM
  3. Do loop not running
    By hattrick_123a in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-21-2011, 08:36 PM
  4. Loop not running
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2006, 10:26 AM
  5. For Each Loop - running three times
    By Rick A in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-23-2006, 01:30 PM

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