+ Reply to Thread
Results 1 to 8 of 8

Macro to copy selected sheets based on filtered cell values to new workbook

  1. #1
    Registered User
    Join Date
    12-03-2020
    Location
    London
    MS-Off Ver
    2016
    Posts
    4

    Macro to copy selected sheets based on filtered cell values to new workbook

    I'm writing a code that once you click on one button, it:

    Filters values in a table based on a combobox value (the first column of these filtered value is the name of my sheets)
    Creates a new blank workbook named from the combobox & another cell in my workbook
    Select all the sheets whose names are presents in the filtered value ("C" Column)
    Copy Paste the selected sheets in the workbook created in 2
    Saves and closes workbook 2
    back to workbook 1 and removes filters
    I can't seem to make 3) and 4) working, I either copy all sheets (regardless of the filter), either (current code) copies only the last one.

    Please see the code below, thanks in advance!

    Please Login or Register  to view this content.

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

    Re: Macro to copy selected sheets based on filtered cell values to new workbook

    It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file. Do you want to save the new workbook in the same folder as the workbook containing the macro? If not, what is the full path to the save folder?
    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-03-2020
    Location
    London
    MS-Off Ver
    2016
    Posts
    4

    Re: Macro to copy selected sheets based on filtered cell values to new workbook

    Apologies I forgot to add it, please find a document attached.
    For now I save it directly to the users' default path, but ideally would always target the mydocuments one.
    Attached Files Attached Files

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

    Re: Macro to copy selected sheets based on filtered cell values to new workbook

    Try this macro. Change the save folder path (in red) to suit your needs.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-03-2020
    Location
    London
    MS-Off Ver
    2016
    Posts
    4

    Re: Macro to copy selected sheets based on filtered cell values to new workbook

    Thanks a lot for this!
    When I add the code to my non-example book, I get a "Subscript out of range" error once it has copied the sheets in the new workbook. Any idea where that could come from?


    Meanwhile as I couldn't make the SpecialCells function work, I coded something without having to filter, rather looping through a range of cells to then copy the sheets.
    My code was less clean than yours but I will add it below as a second solution if anyone comes across this problem!

    Please Login or Register  to view this content.
    Last edited by Galdranxsl; 12-04-2020 at 12:03 PM.

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

    Re: Macro to copy selected sheets based on filtered cell values to new workbook

    When you got the error, which line of code was highlighted when you click "Debug"?

  7. #7
    Registered User
    Join Date
    12-03-2020
    Location
    London
    MS-Off Ver
    2016
    Posts
    4

    Re: Macro to copy selected sheets based on filtered cell values to new workbook

    this is the one that's causing issue.

    wb1.Sheets(InvName.Value).Copy Before:=wb2.Sheets(1)

    Once the macro stops working I can still see that the right tabs have been copied to the other document though, it's like I get the error once it has done the last value in the For loop.

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

    Re: Macro to copy selected sheets based on filtered cell values to new workbook

    In the sample file you posted, the last used cell column C is C16 with no data in any column anywhere below row 16. Is this the case in your actual file?
    Try this revised version:
    Please Login or Register  to view this content.
    b

+ 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. Replies: 1
    Last Post: 05-04-2019, 11:09 PM
  2. [SOLVED] Copy paste values to another workbook based on multiple cell values
    By Bazinga in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-23-2014, 12:41 PM
  3. VBA Code to Move and/or Copy Sheets out to a New Workbook based on Cell Values
    By lhickerson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-18-2014, 09:45 AM
  4. Copy filtered Data into another worksheet based on selected criteria
    By TheresaHartigan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-27-2014, 09:39 AM
  5. Save a copy of the workbook including only sheets with values in a certain cell
    By Melisendk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-11-2013, 08:47 AM
  6. Macro to copy paste values of a Row based on the cell selected in that row
    By boscoamd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-13-2012, 04:56 AM
  7. Replies: 4
    Last Post: 09-15-2012, 02:18 PM

Tags for this Thread

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