+ Reply to Thread
Results 1 to 8 of 8

loop through specific worksheets instead of the entire workbook

  1. #1
    Registered User
    Join Date
    12-20-2014
    Location
    West Coast USA (PST/PDT)
    MS-Off Ver
    2016 / o365
    Posts
    36

    loop through specific worksheets instead of the entire workbook

    Hi there:

    I'm struggling trying to figure out how to loop through a known set of worksheets instead of the entire workbook. Our workbook has about 40 sheets in it, and I need to "do stuff" in about a third of them beginning at a specific row. It seems awfully inefficient to loop through all 40.. however, because I have a stop point to move to the next sheet, I'm not sure if cutting this down will make a difference, and if not, then I won't bother, however, might be good for me as an exercise to learn more.

    I create an array where the element's position refers to the sheet's codename and the value of the element is where the "do stuff" happens.

    If looping through the 1/3 worksheets would make this faster, then I'm thinking the solution would be a 2D array, with the sheet's codename and the starting row. However, 1) I don't think that can be done without having access to vbproject something or other (which we don't want), based on what I've read (one of the drawbacks to working with a sheet's CodeName), and 2) the array would be ugly long, since you cannot do the continuing line inside curly braces. The latter is silly, I know :p

    Here's the code I'm using, I cut out a bunch of other stuff to make it easier to read; there are various conditions to handle different kinds of STUFF, and other what nots, error checks, etc.

    Please Login or Register  to view this content.
    Thank you for your time and review.

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

    Re: loop through specific worksheets instead of the entire workbook

    I'm not sure if this would help. Change the sheet names in the array to suit your needs. In place of the MsgBox you can place your code applicable to each sheet.
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    12-20-2014
    Location
    West Coast USA (PST/PDT)
    MS-Off Ver
    2016 / o365
    Posts
    36

    Re: loop through specific worksheets instead of the entire workbook

    Thank you. Unfortunately, that requires using sheet names, not CodeNames. Some of the sheet names are dynamic. The alternates are using the sheet index, however, we do move around the worksheets.

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: loop through specific worksheets instead of the entire workbook

    Another option using the codename
    Please Login or Register  to view this content.
    where the values in red are the row numbers

  5. #5
    Registered User
    Join Date
    12-20-2014
    Location
    West Coast USA (PST/PDT)
    MS-Off Ver
    2016 / o365
    Posts
    36

    Re: loop through specific worksheets instead of the entire workbook

    Woo hoo, that works!

    I also tested it with straight up codename, without the .Name, and it works for me, example:

    Please Login or Register  to view this content.
    Is there any reason this is NOT a good idea, to have an array of sheets, vs. sheet names?

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: loop through specific worksheets instead of the entire workbook

    Doing it like that is fine I'm used to using the codenames in a For Each loop like Mumps1 did & for that to work you need to have the .Name part, in order for it to work.

  7. #7
    Registered User
    Join Date
    12-20-2014
    Location
    West Coast USA (PST/PDT)
    MS-Off Ver
    2016 / o365
    Posts
    36

    Re: loop through specific worksheets instead of the entire workbook

    OH, that's how you get the foreach to work! I'm going to play around with this to work for my specific code, I suspect the regular for loop is going to be easier because of the "step"; but the foreach is a little cleaner.

  8. #8
    Registered User
    Join Date
    12-20-2014
    Location
    West Coast USA (PST/PDT)
    MS-Off Ver
    2016 / o365
    Posts
    36

    Re: loop through specific worksheets instead of the entire workbook

    FWIW, the change to loop only through the specific worksheets to is not materially faster - don't notice it at all. Well, it was a fun exercise and I learned a few more tricks

+ 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] VBA code to create a workbook and copy or move worksheets in to workbook through loop
    By anishkumarvs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-16-2015, 05:40 AM
  2. [SOLVED] For loop through specific Worksheets
    By mason0567 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-18-2015, 02:10 PM
  3. Replies: 0
    Last Post: 07-24-2014, 09:11 AM
  4. How to delete entire row if first cell of it is empty for specific range and worksheets
    By roshanvmech in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-01-2014, 12:36 PM
  5. Loop for adding specific worksheets in specific workbooks
    By pankaj8219 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-13-2013, 10:22 AM
  6. How to loop through specific worksheets?
    By svenk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-11-2010, 12:15 PM
  7. Loop through specific worksheets
    By Ben in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-27-2005, 10:40 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