+ Reply to Thread
Results 1 to 9 of 9

A macro to create a new workbook and copy data

  1. #1
    Registered User
    Join Date
    09-29-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    50

    A macro to create a new workbook and copy data

    Hello Excel Forum members,

    I was wondering if anyone could assist me with my issue. I'm trying to write a macro that would copy the data based on the value in U1 cell (All Cases tab), paste it into a new workbook and save that workbook based on the value in W1 cell. So say if I input 13 in U1 the macro would copy the data from A1 to R5, paste it into a new workbook and save it under ID 13 name. The new workbook would look like ID 13 tab.

    Thank you for your help!
    Attached Files Attached Files

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: A macro to create a new workbook and copy data

    Perhaps a macro like this?

    Please Login or Register  to view this content.

    Macro uses auttofilter and value from U1 as criteria for filtering. The filtered value are copie to a new sheet named after the value in W1. Then macro creates a new workbook using the new sheet and save this as an xlsx file.

    At the moment this file is saved to folder "C:\Temp\ so you need to change this to the folder of your choise. Aftes closing the xlsx file the added sheet is deleted and autofilter removed from sheet "All cases".

    Maro asumes that the master file only contains sheet "All cases"

    Alf
    Last edited by Alf; 06-02-2016 at 05:02 PM.

  3. #3
    Registered User
    Join Date
    09-29-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: A macro to create a new workbook and copy data

    Hi Alf,

    Thank you so much for your help. The macro is working. I also have a follow-up question- what if I create a second tab (All Ids) where I'd store every single ID in the population (13,45,55,101 and s on and so forth)- would that be possible to create a macro that just runs those IDs one by one in a single command and saves them in the corresponding directory ?

    Thank you!
    Attached Files Attached Files

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: A macro to create a new workbook and copy data

    would that be possible to create a macro that just runs those IDs one by one in a single command and saves them in the corresponding directory ?
    Yes that should be no problem. What I would suggest is a macro that starts by copying all values from the A column to an new sheet then extracts the unique values to a list. Macro then loops through this list and setting an autofilter based on the values. Then adds a new sheet with the filtered values and save that as a new file named "ID " + cell value. Delets this sheet and goes to the next value.

    Finally sheet with unique values are deleted and autofilter removed from sheet "All cases". As mention earler you need to change the fPath strin to a folder of your choice.

    Please Login or Register  to view this content.
    Alf

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: A macro to create a new workbook and copy data

    @ Alf

    It would be much easier and cleaner to create Dictionary with unique elements and loop through that to autofilter.
    Also use second sheet to copy values to and copy to new workbook. Just clear and rename second sheet for next Autofilter action.
    Take a look at code in Module1 I made some time ago for someone at this forum. It's somewhat similar to this question.
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Registered User
    Join Date
    09-29-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: A macro to create a new workbook and copy data

    Alf,

    Thanks again for the suggestion. The macro saves all the data of each unique ID number as a separate files. However, for some reason only the first ID on the list (ID 13) gets fully copied over. The rest of the ID's have their corresponding files created but without any information. In addition, when I first ran the macro it gave me an error on this line:

    ActiveSheet.Name = "All IDs"

    I changed it to ActiveSheet.Name = "Sheet1" and it seemed to work except for the issue described above.

    Thank you again for your help!

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: A macro to create a new workbook and copy data

    Before running code adjust fPath to need !!
    Please Login or Register  to view this content.

  8. #8
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: A macro to create a new workbook and copy data

    error on this line:

    ActiveSheet.Name = "All IDs"
    As the macro creates a sheet named "All IDs" there can be no sheet in the "Master file" with the same name. That maked the macro throws an error as you got so you need to remove that sheet.

    Asfor the empty files well that's my bad. You need to add a line activating the "All cases" sheet so change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    then the macro should run without any problem.

    I would also sugges you test the code from bakerman2 as his code seems better than mine.

    hi bakerman2 just had a quick look at your code seems shorter (better) than mine will have a closer look later this evening.

    Alf

  9. #9
    Registered User
    Join Date
    09-29-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: A macro to create a new workbook and copy data

    Alf and bakerman2,

    Thank you for your help! Your macros are working fine. Sorry for the late reply-was out of pocket.

+ 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 can I copy data from one workbook and create another workbook?
    By anmol0611 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-08-2015, 11:54 PM
  2. How to copy a data from another Workbook and create a graph in different workbook?
    By somani123 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-02-2015, 12:22 PM
  3. [SOLVED] Macro to copy sheets from workbook and create new workbook
    By sudric in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-18-2015, 11:53 AM
  4. Create Copy of workbook using macro and Name them as per list
    By VJOSHI in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-31-2014, 07:27 AM
  5. VBA Create new workbook and copy paste data
    By jayt91 in forum Excel General
    Replies: 8
    Last Post: 09-05-2014, 09:46 PM
  6. Copy data from workbook, create new workbook, paste data to new worbook?
    By mpkavanagh in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-16-2011, 06:39 PM
  7. Macro to Create a set of folders and copy the active workbook
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-02-2009, 07:12 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