+ Reply to Thread
Results 1 to 15 of 15

Can you group worksheets together, and call upon them all at once within a VBA code?

  1. #1
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Question Can you group worksheets together, and call upon them all at once within a VBA code?

    Hi again!

    Okay so I was wondering if you amazing excel-guru's could help me out--bear with me while I try to word this properly.

    I have a bunch of sheets in a workbook (Named A, B, C, ... etc) that all contain different data but in identical tables (one on each page)

    I have a user form that you can choose which sheet you want data from, and the code copies data from chosen sheet into a "main table".
    The combo box that you choose your sheet from is named 'txtName' and the options are merely "A, B, C, ... etc", same as the sheet names.

    Please Login or Register  to view this content.
    Okay, so now I want to dive deeper.
    I want to have a combo box with 'group' choices. Say Group 1 is for Sheets A & B, and Group 2 is for Sheets C & D

    How would I write the code so that if I choose Group 1, excel will go copy/paste the data from Sheets A AND B into the main table? Is there a way to define a worksheet 'object' as being sheet A AND sheet B so I can keep the code the same?

    I don't have a mock-file created to show exactly what I mean but can try to make one if my explanation is too difficult to understand!!!
    Last edited by sy898661; 06-05-2019 at 03:01 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Is there a way to define a worksheet 'object' as being sheet A AND sheet B so I can keep the code the same?
    No. The only way to do this is to change your code to know which sheets are in which groups, and do two copy operations.

    ETA: You might be able to define a discontiguous range over two sheets and do one physical copy, but I've never tried it. Regardless, you still have to map groups to sheets in the code.
    Last edited by 6StringJazzer; 06-05-2019 at 11:33 AM.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,902

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Try using Sheets(Array("Sheet1", "Sheet2")) then loop through worksheet object.

    AFAIK, you can't copy from multiple sheets in one shot.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  4. #4
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Quote Originally Posted by 6StringJazzer View Post
    No. The only way to do this is to change your code to know which sheets are in which groups, and do two copy operations.
    Okay thank you so much!

  5. #5
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Quote Originally Posted by CK76 View Post
    Try using Sheets(Array("Sheet1", "Sheet2")) then loop through worksheet object.

    AFAIK, you can't copy from multiple sheets in one shot.
    Yes okay perfect!! I will try that! Thank you!

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,262

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Make use of sheet to store Groups...See attached...Loop through sheets in stored Group
    Attached Files Attached Files
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  7. #7
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Quote Originally Posted by sintek View Post
    Make use of sheet to store Groups...See attached...Loop through sheets in stored Group
    YES thank you so much this is exactly what I was trying to do!!!

  8. #8
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Quote Originally Posted by sintek View Post
    Make use of sheet to store Groups...See attached...Loop through sheets in stored Group
    Okay so now I'm getting a script out of range error.. when I look at my other code that is the same (with different variable names) there is no error! I tried renaming the first String and it still wont run.
    Please Login or Register  to view this content.

  9. #9
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,902

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Why string? String can only be used to set single worksheet object. If it's delimited string, you can use split to generate array from string...

    There are few ways to loop through specific worksheets, some examples below.
    Please Login or Register  to view this content.
    Pick whichever method that suites your taste and needs.

  10. #10
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Quote Originally Posted by CK76 View Post
    Why string? String can only be used to set single worksheet object. If it's delimited string, you can use split to generate array from string...

    There are few ways to loop through specific worksheets, some examples below.

    Pick whichever method that suites your taste and needs.
    I keep getting out of range error
    Last edited by jeffreybrown; 06-05-2019 at 01:32 PM.

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Hi sy898661,

    Please do not quote whole posts as it just cause clutter.
    HTH
    Regards, Jeff

  12. #12
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    This is the code I have, the error keeps popping up when it hits the line
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,902

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Couple of things...
    Below part should reside outside For... loop.
    Please Login or Register  to view this content.
    "ws1" ... I don't see this variable being defined in your code. Should it not be "ws"?

  14. #14
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    Ahh yes, I see that now *facepalm*

    Fixed the ws and now it works
    Thank you!!!

  15. #15
    Registered User
    Join Date
    07-24-2018
    Location
    California
    MS-Off Ver
    10
    Posts
    56

    Re: Can you group worksheets together, and call upon them all at once within a VBA code?

    question was basically a new question so I am moving it to a new thread I hope that's okay please let me know if it is not and I will never do that again!
    Last edited by sy898661; 06-05-2019 at 03:29 PM. Reason: thread too busy... i will post a new one since this is kind of a new question

+ 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. How do I group worksheets (Lotus 123 function is "Sheet>Group Shee
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-06-2005, 05:05 PM
  2. [SOLVED] How do I group worksheets (Lotus 123 function is "Sheet>Group Shee
    By jaking in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 PM
  3. [SOLVED] How do I group worksheets (Lotus 123 function is "Sheet>Group Shee
    By jaking in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 11:05 AM
  4. [SOLVED] How do I group worksheets (Lotus 123 function is "Sheet>Group Shee
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2005, 06:05 AM
  5. How do I group worksheets (Lotus 123 function is "Sheet>Group Shee
    By Dave Peterson in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-06-2005, 05:05 AM
  6. How do I group worksheets (Lotus 123 function is "Sheet>Group Shee
    By jaking in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  7. [SOLVED] How do I group worksheets (Lotus 123 function is "Sheet>Group Shee
    By jaking in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  8. How do I group worksheets (Lotus 123 function is "Sheet>Group Shee
    By jaking in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 PM

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