+ Reply to Thread
Results 1 to 25 of 25

Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

  1. #1
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    I'm hoping this is an easy fix. The macro will not select the next sheet "ws" in ThisWorkbook. The message I get on the highlighted line of code is "Method 'Select' of object'_Worksheet' failed" on the attempt at a second run through the code; everything works fine tthr first time through.

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    12-07-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2021
    Posts
    126

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    I think you would not need to use
    Please Login or Register  to view this content.
    but

    Please Login or Register  to view this content.
    Since now it is read as
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Remove the With ws and the End With lines

  4. #4
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Quote Originally Posted by ArnolddG View Post
    I think you would not need to use
    Please Login or Register  to view this content.
    but

    Please Login or Register  to view this content.
    Since now it is read as
    Please Login or Register  to view this content.
    I'm getting the same error message with this.

    Below is the section of code I changed.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Quote Originally Posted by stnkynts View Post
    Remove the With ws and the End With lines
    I'm getting the same error message here as well.

    Below are the sections of code I changed.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Quote Originally Posted by AB33 View Post
    Please Login or Register  to view this content.
    I'm now receiving the following message at the highlighted line of code. "Method 'Range' of object'_Worksheet' failed"

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Please do not reply with quote.
    Please Login or Register  to view this content.
    A dots are missing. Check my code

  9. #9
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Great! Now, I'm debugging at the below edited line of code. I assumed I needed to add the dots in front of "Range" here as well. This works for the first pass but on the second loop it's debugging. It also appears to be creating the Ranges based on the first sheet (rather than the second) in the second loop so I don't think it's actually going to the "Next ws"

    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Please Login or Register  to view this content.
    If it works for the first sheet, it is because it found "CHEMICALS". If it does not found, it will error.
    Please note this is not a complete code, just merely to give you an idea how to set the find function.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Thanks a lot AB33 for your help thus far. I think we're close however if "CHEMICALS" can't be found I have other problems then. The key words are exactly the same for each tab and each tab has all of them in the same columns. The debugging message I'm receiving is: "Run-time error '91': Object variable or With block variable not set" What I've noticed is the .Range lines of code will work on whatever Sheet I have currently selected and NOT loop to the next Sheet unless I manually select it. Therefor it's trying to repeat the same code over the same ranges. If I hover over the highlighted section of code below, VBA shows me the correct range of (in this case) L11:L38.

    Please Login or Register  to view this content.
    I'm not sure why the code works the first time on a set of ranges and then bugs out on the second loop over the same set of ranges, regardless of the fact that it isn't looping to the "Next ws", but that's question I'm not sure I need answered at this time.

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Could you please attach the sample book?

  13. #13
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    I noticed the code will only run on the first tab but will reference the ranges for whatever Sheet is the active sheet.
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Okay!
    A couple of points on your code.
    It is a good practice to use Option Explicit in your code(At the top). Since mine is default set, I get an error( Not defined variable). You have lots of undefined variables.
    The code loops through all sheets, including hidden sheets. You have 3 sheets in the collection, but 2 are shown. Since the code loops through all sheets, it errors on the hidden sheet.

  15. #15
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    ABB33, thank you for the advice. I'm not sure how to define "CHEMICALS" and "COAL" in this very short sample which I need to do if I'm going to use Option Explicit. I also noticed VBA was counting 3 sheets but I don't know where the third sheet is; if you try to unhide it, there's nothing to unhide so I don't know how VBA comes up with 3 sheets. Finally, I still don't know why VBA wont move to the "Next ws". The commands under 'CREATE HEADER are very basic and should be executed on the "Next ws" after the first loop but that isn't happening which leads me to beleive that's were the real breakdown is occuring; VBA isn't actually moving to the next sheet.

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

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    CHEMICALS and COAL should be declared as Long, or Variant.

    PS If you are unclear of which data type to use in a declaration then use Variant, but remember to change that to a specific data type when you work out which one to use.

    PPS Instead of using the [] notation try using Range.
    If posting code please use code tags, see here.

  18. #18
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Thanks Norie!

    This is now my code and it still debugs at the highlighted code on the second pass through. "Object variable or With block variable not set"

    Please Login or Register  to view this content.

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

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    That means that Find isn't finding 'CHEMICALS' in column C of the current worksheet you are looping through.

    Now that doesn't actually mean it's not there, the problem could be with Find.

    You could try using Match to find the row 'CHEMICALS' (or 'Chemicals') is in and then use that in the code to place the formula.
    Please Login or Register  to view this content.

  20. #20
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    The code loops through the sheets, except the hidden sheet (Please check my last post). If the loop skips that(Which is hidden) sheet, the code does not error and you should be able to see data in to visible sheets though, I do not know what the code does.
    As I said on previous post, the code will error if it could not find the searched item, hence you need to built error trap on the find function.

  21. #21
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    Okay, so I took the code I had and added it to a new worksheet with just 2 tabs (which is what I thought I had to begin with) and now the code will loop fully twice, which is for the number of sheets I have in this file. It works great but for one exception. My ranges which are defined by the location of "CHEMICALS" and "COAL" are the ranges for whichever Sheet I have as the active sheet when I run the macro; so the range is not corrrect for one of the sheets buts works perfectly for the other. Is there a better alternative for what I'm trying to accomplish than the following?

    Please Login or Register  to view this content.

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

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    You need to specify the sheet name in the formula(s) you are evaluating, or not use Evalute.
    Please Login or Register  to view this content.
    By the way, you can write the ranges for the formula like this,
    Please Login or Register  to view this content.
    no need for this.
    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    I'm getting an "Application-defined or object-defined error" with the following line of code.

    Please Login or Register  to view this content.

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

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    That's not the code I posted, there's ) in the wrong place.

    And there's an extra ) in what I posted.
    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    08-24-2012
    Location
    Columbus
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: Code to Loop through Each Worksheet in ThisWorkbook.Worksheets

    AWESOME! Thank you so much Norie and AB33 for all of your help on this one! It's working like a charm.

+ 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. [SOLVED] Loop/compile data from different worksheets into a single worksheet
    By jasondu in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-19-2012, 02:57 PM
  2. Loop through one worksheet and create new worksheets
    By dd1313 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2012, 07:56 PM
  3. Loop code for certain worksheets in workbook
    By longbow007 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-18-2010, 01:05 AM
  4. Loop code for selected worksheets
    By longbow007 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2010, 12:14 AM
  5. Need to loop code for certain worksheets in workbook
    By longbow007 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-28-2010, 08:00 AM

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