+ Reply to Thread
Results 1 to 14 of 14

Copy Sheets to another Workbook based on MultiSelect User Form

  1. #1
    Forum Contributor
    Join Date
    12-29-2012
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    325

    Copy Sheets to another Workbook based on MultiSelect User Form

    So I have a macro that traditionally cycled through a folder of workbooks and copied the first sheet of each workbook into the "master macro workbook."

    Now I need that macro to be able to use a user form where the user can select multiple sheets for each workbook and then copy that into the master macro workbook.

    I have the user form that populates with the list of sheets and a user can select multiple sheets, however, I don't really know how to pass those selections on to the rest of my macro which then would copy those sheets into the master macro workbook.

    Thanks for the help.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    It helps to knoiw what kind of control you are using on the user form to list the sheets and what the name of the control is.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    12-29-2012
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    325
    Quote Originally Posted by JLGWhiz View Post
    It helps to knoiw what kind of control you are using on the user form to list the sheets and what the name of the control is.
    Sorry, so a general UserForm and a ListBox called ListBox1 that has the multi-select turned on. Also a command button.

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    I am a little rusty on the list boxes and user forms but i think something like this in the UF code module might work.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    12-29-2012
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    325

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    Quote Originally Posted by JLGWhiz View Post
    I am a little rusty on the list boxes and user forms but i think something like this in the UF code module might work.
    Please Login or Register  to view this content.
    Thanks for the reply. That works great, however, I get a subscript out of range if I try to multi-select two or more worksheets? I thought your code iterates through each selection in the list?

  6. #6
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    It should loop through the list. Check the spelling of the names in the list against the sheet tab names.

  7. #7
    Forum Contributor
    Join Date
    12-29-2012
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    325

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    Quote Originally Posted by JLGWhiz View Post
    It should loop through the list. Check the spelling of the names in the list against the sheet tab names.
    I'm not sure if it's spelling since if I choose one sheet (Sheet1) for example, it continues on to the next workbook. I can do the same for another sheet (Sheet2) and it works. It gives me the subscript out of range when I multi-select Sheet1 and Sheet2 which I don't get from looking at you code. It hits me with that error on the copy sheet line.

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

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    Try
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    12-29-2012
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    325

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    Quote Originally Posted by Fluff13 View Post
    Try
    Please Login or Register  to view this content.

    I get the same error and it won't even let me choose one worksheet without throwing the error.

    I tried ActiveWorkbook instead, and it gave me the original result - Allows me to choose one worksheet, but errors out when sheets are multi-selected.

    My UserForm code has the following code for the Initialize UserForm:

    Please Login or Register  to view this content.
    Again, sorry I'm not very familiar with how the UserForm functions work.

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

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    Ok, how about
    Please Login or Register  to view this content.
    Last edited by Fluff13; 07-23-2019 at 08:52 AM.

  11. #11
    Forum Contributor
    Join Date
    12-29-2012
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    325

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    Quote Originally Posted by Fluff13 View Post
    Ok, how about
    Please Login or Register  to view this content.
    Interestingly enough that works!

    I'm a little perplexed when ActiveWorkbook added on to the beginning of the error line didn't work but the variable Wbk did work when it equals ActiveWorkbook.

    Anyway thanks so much!

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

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    You're welcome & thanks for the feedback.

    I suspect that when the first sheet copied over, the other workbook became the active workbook & so it was trying to copy from the wrong workbook.

  13. #13
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    Quote Originally Posted by nobodyukno View Post
    Interestingly enough that works!

    I'm a little perplexed when ActiveWorkbook added on to the beginning of the error line didn't work but the variable Wbk did work when it equals ActiveWorkbook.

    Anyway thanks so much!
    As Fluff pointed out (and I should have realized) when the 'Sheet.Copy' method is used, the destination workbook becomes the active workbook. Using 'ActiveWorkbook' Inside the loop will have the same effect as not using any parent reference, i.e. it points to the workbook made active by the 'Sheet.Copy' method. But by putting the ActiveWorkbook (original) into a variable before the loop begins, the variable holds the value even after the 'Sheet.Copy' method processes.

    Thanks for the reps,
    Regards, JLG

  14. #14
    Forum Contributor
    Join Date
    12-29-2012
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    325

    Re: Copy Sheets to another Workbook based on MultiSelect User Form

    Quote Originally Posted by Fluff13 View Post
    You're welcome & thanks for the feedback.

    I suspect that when the first sheet copied over, the other workbook became the active workbook & so it was trying to copy from the wrong workbook.
    Quote Originally Posted by JLGWhiz View Post
    As Fluff pointed out (and I should have realized) when the 'Sheet.Copy' method is used, the destination workbook becomes the active workbook. Using 'ActiveWorkbook' Inside the loop will have the same effect as not using any parent reference, i.e. it points to the workbook made active by the 'Sheet.Copy' method. But by putting the ActiveWorkbook (original) into a variable before the loop begins, the variable holds the value even after the 'Sheet.Copy' method processes.

    Thanks for the reps,
    Regards, JLG
    I actually have one more question:

    Is there a way to make the Workbooks("master macro workbook") piece to be dynamic in case the workbook name is different? I have a variable for that workbook in my main macro module code but I don't know how to dynamically assign it in the UserForm code?

    EDIT: Nevermind didn't know global variables can also apply to UserForms.

    Thanks again!
    Last edited by nobodyukno; 07-23-2019 at 12:02 PM.

+ 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. Trying to copy a set of sheets and rename them based on user input
    By Pawtang in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-07-2019, 02:49 PM
  2. Replies: 3
    Last Post: 02-16-2018, 06:40 PM
  3. [SOLVED] Filling multiselect listbox based on closed workbook data
    By FKurzhals in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-23-2013, 02:56 PM
  4. Replies: 4
    Last Post: 09-15-2012, 02:18 PM
  5. User Form List items to link to other sheets in the workbook
    By ravergirl7216 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2010, 02:00 PM
  6. Copy 5 sheets to new workbook with dialog form where to save
    By Josh_123456 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2008, 09:11 AM
  7. Copying template to workbook but user form does not copy?
    By Alan Ibbotson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2005, 07:10 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