+ Reply to Thread
Results 1 to 18 of 18

Worksheet Looping not working for all the sheets

  1. #1
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Worksheet Looping not working for all the sheets

    Hi All,

    I am trying to run below Macro for several worksheets in a workbook (For an instance, lets assume 3 sheets)

    Though the initial sorting part is getting executed for all the three worksheets but rest of code is not.

    Please suggest what changes need to be carried out in the code below so as to make it worksheets friendly/usable.

    Code :
    ----------------------------------------------------------------------------
    Please Login or Register  to view this content.
    --------------------------------------------------------------------
    Last edited by Blue_kul; 01-02-2012 at 08:17 PM. Reason: Code tag were missing

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Worksheet Looping not working for all the sheets

    Let me guess, the second half of the code is only being executed in the first sheet but not the rest
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Worksheet Looping not working for all the sheets

    Hi Blue_kul, can you provide a mock workbook with some non-sensitive data for us to work with?
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  4. #4
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    Yups ... And I am unable to get through it !!

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Worksheet Looping not working for all the sheets

    I think you need to declare what sheet is being worked on regarding your code. Try:
    Please Login or Register  to view this content.
    and let us know how it goes.
    Last edited by Mordred; 12-29-2011 at 10:35 AM.

  6. #6
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    here it is, first sheet shows the desired result but not working on rest of two sheets

    Test File.xlsm
    Last edited by Blue_kul; 12-29-2011 at 10:35 AM.

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Worksheet Looping not working for all the sheets

    From a quick look at your code, you are looping correctly through the worksheets, but the problem is that the focus of the second part of the code never moved from the first sheet. The way I would normally do this is to use:

    Please Login or Register  to view this content.
    but that also means that you have to put a period before every range [e.g. .Range("A1") or .Cells(1, 1)].

    Or you can activate each worksheet at the begining of the loop:

    Please Login or Register  to view this content.
    abousetta

  8. #8
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    Nopes ... no luck !! i think I am missing something very stupid .. !!

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Worksheet Looping not working for all the sheets

    Activating each sheet is slow and makes the screen jump about (unless you set Application.ScreenUpdating=False), it's best to use something along the lines of:

    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Worksheet Looping not working for all the sheets

    Hi Andrew,

    I agree that activating, selecting, etc. is a waste of time but it is a valid option especially for a novice who might not be able to spot every range object (especially if the code is snippets borrowed from online pieces of code). It took me some time and effort to learn how to improve code efficiency and it's not always clear which lines of code can be/ should be ommited or condensed.

  11. #11
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    Quote Originally Posted by abousetta View Post
    From a quick look at your code, you are looping correctly through the worksheets, but the problem is that the focus of the second part of the code never moved from the first sheet. The way I would normally do this is to use:

    Please Login or Register  to view this content.
    but that also means that you have to put a period before every range [e.g. .Range("A1") or .Cells(1, 1)].

    Or you can activate each worksheet at the begining of the loop:

    Please Login or Register  to view this content.
    abousetta
    I tried the second option but still no luck, only data gets sorted but rest of action is missing.
    If possible can you please evaluate my worksheet, attached in earlier post, for any issue with properties or something minute I am missing,

  12. #12
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    Tried the code, separately on each sheet and is working fine, but giving me error, when tried to run for sheet 3, its says, script out of range, though activeworksheet count is rightly captured as 3.

  13. #13
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    And now when i tried to loop in worksheets by using activate command, it throws me error at the last sheet always, can you spot any issue with a loop/counter, coz i have tried adding/deleting sheets but at last counter always errors out.

    Error Message:
    Capture.jpg

    When I tried to debug using run to cursor, it points to
    Capture2.JPG

  14. #14
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    Any one please ? I am still stuck at this and it wont loop through all my worksheets !! Thanx

  15. #15
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Worksheet Looping not working for all the sheets

    Can you post the code you're now using, please, and I'll have a look.

  16. #16
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    Finally, I got it working .. thanks all for your support !!!

    How do I mark a thread to resolved/solved ?

  17. #17
    Registered User
    Join Date
    12-29-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: Worksheet Looping not working for all the sheets

    I looped all my worksheets using:
    Please Login or Register  to view this content.

  18. #18
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Worksheet Looping not working for all the sheets

    Hi,

    Glad it all worked out. You can mark it as [SOLVED] by going to the original post (#1) --> Go Advanced --> and change the prefix to [SOLVED]. Details should be avaialble in the forum rules.

    Best of luck.

    abousetta

+ 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