+ Reply to Thread
Results 1 to 16 of 16

Selecting Mulitple Sheets by their Code Index Names

  1. #1
    Registered User
    Join Date
    02-11-2013
    Location
    Guernsey
    MS-Off Ver
    Excel 2010
    Posts
    80

    Selecting Mulitple Sheets by their Code Index Names

    Hi,

    I know how to select multiple sheets within VBA via the

    Please Login or Register  to view this content.
    and i know how to select a single sheet via its code index name

    Please Login or Register  to view this content.
    but how do you select multiple sheets via their code index name??

    I need this because the sheet name is not static and will continue to change depending on the sheet contents and so i cannot specify the sheets name in a VBA select, i need to use the Code Index Name

    Many Thanks

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Selecting Mulitple Sheets by their Code Index Names

    Please Login or Register  to view this content.
    ?

    Edit: I misunderstood, I'm not sure on the best way but you could select them one at a time with replace set to false, for example:
    Please Login or Register  to view this content.
    Last edited by ragulduy; 05-31-2013 at 04:02 AM.

  3. #3
    Registered User
    Join Date
    02-11-2013
    Location
    Guernsey
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: Selecting Mulitple Sheets by their Code Index Names

    Ah thats even easier as it selects from their order in the workbook, just as long as no one ever comes along and shifts the order around!!

    I have 80 to select so dont fancy sticking in 80 ",", do you know how i can get around this? tried 1:80 and 1 to 80 but didnt work

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Selecting Mulitple Sheets by their Code Index Names

    How about:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-11-2013
    Location
    Guernsey
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: Selecting Mulitple Sheets by their Code Index Names

    Thanks, glad to have some options

    I think the safest way to avoid any future codings errors from someone messing around with tabs is to select each sheet with its specific index name rather than its position in the workbook

    Although this requires a long list of code

    I will have to weigh up the pros and cons of each method but thanks for the help

    Simon

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Selecting Mulitple Sheets by their Code Index Names

    It's a bit convoluted but you could probably use a loop like this if all your sheet codenames are Sheet1, Sheet2 etc.

    Dim wS
    For Each wS In ActiveWorkbook.Sheets
    If Replace(wS.CodeName, "Sheet", "") <= 80 Then wS.Select False
    Next

  7. #7
    Registered User
    Join Date
    02-11-2013
    Location
    Guernsey
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: Selecting Mulitple Sheets by their Code Index Names

    Ah ok i know what your trying to do their but i have other sheets in the workbook which i dont want to select so i would need to rename their codename and remember to do that for any new sheets that may be added in the future.

    I think i will have to stick with the safest route as follows:

    Please Login or Register  to view this content.

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

    Which sheets do you not want to select?

    PS What are you doing with the selected sheets?
    If posting code please use code tags, see here.

  9. #9
    Registered User
    Join Date
    02-11-2013
    Location
    Guernsey
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: Selecting Mulitple Sheets by their Code Index Names

    Hi,

    Whilst working more on it i could actually do with some sort of Loop Function as i need to select sheet 1 then run a macro then select sheet 2 run the same macro and so on so my code curently is:

    Please Login or Register  to view this content.
    Seems a bit horific but it will work!! However if i could build some sort of loop function by using the CodeNames then great

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Selecting Mulitple Sheets by their Code Index Names

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    02-11-2013
    Location
    Guernsey
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: Selecting Mulitple Sheets by their Code Index Names

    So if i interpret that code correctly is does this:

    For each worksheet in workbook

    If CodeName includes "Sheet" then replace with blank to leave just the number

    Then for all CodeNames less than or equal to 80

    Worksheet select

    Call Get_PL

    Next Worksheet


    This seems like it would work as there would never been any sheets added within the range of 1 to 80 so wont affect this code?

  12. #12
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Selecting Mulitple Sheets by their Code Index Names

    Yeah that is what it does, I tried it quickly and it seemed to work.

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Selecting Mulitple Sheets by their Code Index Names

    If you have programmatic access to the VBPorject trusted
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  14. #14
    Registered User
    Join Date
    02-11-2013
    Location
    Guernsey
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: Selecting Mulitple Sheets by their Code Index Names

    Unfortunately this gives me a Type Mismatch?

  15. #15
    Registered User
    Join Date
    02-11-2013
    Location
    Guernsey
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: Selecting Mulitple Sheets by their Code Index Names

    Quote Originally Posted by yudlugar View Post
    Please Login or Register  to view this content.
    I am getting a Next without For error with this one

  16. #16
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Selecting Mulitple Sheets by their Code Index Names

    my bad-need to force conversion
    Please Login or Register  to view this content.
    yudlugar's only needs an 'End If' line after the 'Call Get_PL' line

+ 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