+ Reply to Thread
Results 1 to 19 of 19

Copy data to new workbooks based on cell value

  1. #1
    Registered User
    Join Date
    08-22-2019
    Location
    Washington
    MS-Off Ver
    __
    Posts
    7

    Lightbulb Copy data to new workbooks based on cell value

    Hola

    This is my first post but I've been to this site many times for useful information so thank you! In fact, the code below is based on some code I found here I believe.

    My intention is to create x number of workbooks based on values in column k (RepAssign). X is precisely my hang up. I've been able to write code to filter by values in a column to copy the filtered data to a new workbook, however, the values were static in the past.

    My new code is splitting out records evenly based on user input, a prompt asks the user how many ways to split the rows. For testing, I went with 3 so I'd like to end up with 3 new workbooks for example, leaving the original/source workbook as is. It could be anywhere from 2 to 20 workbooks being created, again depending on user input. See attached for sample data.

    Existing code;

    Please Login or Register  to view this content.
    Thanks!
    Attached Files Attached Files
    Last edited by corndogs; 10-29-2019 at 11:04 AM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,522

    Re: Copy data to new workbooks based on cell value

    Hi corndogs, welcome to the forum.

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    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.

  3. #3
    Registered User
    Join Date
    08-22-2019
    Location
    Washington
    MS-Off Ver
    __
    Posts
    7

    Re: Copy data to new workbooks based on cell value

    Sorry about that! I believe it's adjusted as requested

    Thanks!

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi, try this !


    According to your attachment as a beginner starter to paste to the (test) worksheet module :

    PHP Code: 
    Sub Demo1()
         
    Dim P$, R&
             
    ThisWorkbook.Path Application.PathSeparator
             Application
    .DisplayAlerts False
             Application
    .ScreenUpdating False
        With Me
    .ListObjects(1).Range
               
    .Columns(11).AdvancedFilter xlFilterCopy, , [N1], True
            
    For 2 To [N1].CurrentRegion.Count
                Workbooks
    .Add
                
    [N2].Value2 Cells(R14).Value2
               
    .AdvancedFilter xlFilterCopy, [N1:N2], ActiveSheet.[A1]
                
    ActiveWorkbook.SaveAs P & [N2].Value251
                ActiveWorkbook
    .Close
            Next
        End With
             
    [N1].CurrentRegion.Clear
             Application
    .ScreenUpdating True
             Application
    .DisplayAlerts True
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Registered User
    Join Date
    08-22-2019
    Location
    Washington
    MS-Off Ver
    __
    Posts
    7

    Re: Hi, try this !

    Hi Marc,

    I'm getting a compile error on Me.ListObjects - 'Invalid use of Me keyword'. I've actually never used 'me' in my code so I'm lost. Is there a replacement I can use?

    Thanks!
    J

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,522

    Re: Copy data to new workbooks based on cell value

    Did you put the code in the worksheet-module where the table resides ?

  7. #7
    Registered User
    Join Date
    08-22-2019
    Location
    Washington
    MS-Off Ver
    __
    Posts
    7

    Re: Copy data to new workbooks based on cell value

    Yes, I placed the code right below the 'End With' line.

    Please Login or Register  to view this content.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,522

    Re: Copy data to new workbooks based on cell value

    That's not what I asked.

    Right-click on the sheet tab where the table is on. Select View Code (now you are in the Sheet module). Paste Marc L's code in the right code window.

  9. #9
    Registered User
    Join Date
    08-22-2019
    Location
    Washington
    MS-Off Ver
    __
    Posts
    7

    Re: Copy data to new workbooks based on cell value

    I apologize for the misunderstanding. I store all my macros in one project, labeled ProvMacros. I created a new module for this code within the ProvMacros project named Split Assign and get the compile error. Then based on your feedback, I placed the code in a new the module within the workbook itself instead of the project I always use. I come up with the same compile error.
    Attached Images Attached Images

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,522

    Re: Copy data to new workbooks based on cell value

    No, open Microsoft Excel Objects and double-click on the sheet where your table is on, then paste the code in the right window.

    The code needs to be in a SHEET-module, not a module you create yourself.

  11. #11
    Registered User
    Join Date
    08-22-2019
    Location
    Washington
    MS-Off Ver
    __
    Posts
    7

    Re: Copy data to new workbooks based on cell value

    Thank you bakerman2.

    This does work now but the problem is the source file will change weekly/daily. It wouldn't be ran it on the same file every time so having the code embedded in the spreadsheet won't work for my purposes.

    I'm sure it's my lack of knowledge but my macros are set up as modules I created myself because it has to do with manipulating new files each instance. My intention is to have the end user, who has no knowledge of macros, open the file needing manipulation, then selecting the macro without doing anything in the VBA screen to execute.

    Thanks

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    So you will have same issue whatever the code is located …

  13. #13
    Registered User
    Join Date
    08-22-2019
    Location
    Washington
    MS-Off Ver
    __
    Posts
    7

    Re: Copy data to new workbooks based on cell value

    Well no, it does work when the code is added within the sheet, it's just not the ideal structure for the process we have set up. I was looking for an alternate piece of code to replace Me.ListObjects and in essence end up with the same results.

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    'Me' is a reference to the worksheet where the code is, as you must have read in the VBA inner help …

  15. #15
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,522

    Re: Copy data to new workbooks based on cell value

    Please disregard.
    Last edited by bakerman2; 11-01-2019 at 09:27 PM.

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Maybe an issue :
    Please Login or Register  to view this content.

  17. #17
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,522

    Re: Copy data to new workbooks based on cell value

    Yes, that's why I deleted my previous answer.

    Have to find an other angle.

  18. #18
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Oh no, your previous answer was pretty good except for the red part which must stay as initialy written
    'cause in my original codeline [N1:N2] without any sheet reference not refers to ActiveSheet but to the 'Me' worksheet
    so ActiveSheet.[A1] must stay as it is as it refers to the new workbook, the destination of the copy …
    Last edited by Marc L; 11-01-2019 at 10:14 PM.

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Instead of an easier / safer worksheet class module my revamped code for a general / standard module like 'Module1' :

    PHP Code: 
    Sub Demo1r()
             
    Dim R&
             
    Application.DisplayAlerts False
             Application
    .ScreenUpdating False
        With Sheet8
    .ListObjects(1).Range
               
    .Columns(11).AdvancedFilter xlFilterCopy, , .Range("N1"), True
            
    For 2 To .Range("N1").CurrentRegion.Count
                Workbooks
    .Add
               
    .Range("N2").Value2 = .Cells(R14).Value2
               
    .AdvancedFilter xlFilterCopy, .Range("N1:N2"), [A1]
                
    ActiveWorkbook.SaveAs ThisWorkbook.Path Application.PathSeparator & .Range("N2").Value251
                ActiveWorkbook
    .Close
            Next
               
    .Range("N1").CurrentRegion.Clear
        End With
             Application
    .ScreenUpdating True
             Application
    .DisplayAlerts True
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

+ 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. Copy Range from Workbooks in folder based on Cell Value
    By Cannon26 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2019, 02:07 PM
  2. 2 excel workbooks, same headers, copy data based on criteria
    By imbigsis56pa in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-23-2019, 11:06 PM
  3. [SOLVED] Copy/paste data between workbooks based on criteria in a column.
    By PaulM100 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-17-2019, 11:38 AM
  4. copy data between worksheets from different workbooks based on column name
    By dhroark in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2019, 02:40 PM
  5. [SOLVED] Copy and pasting rows to specified workbooks/sheets based on cell value.
    By darrenpinto in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2018, 02:39 AM
  6. Macro to copy data from other workbooks based on cell value
    By ExcelFailure in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-16-2013, 12:53 AM
  7. [SOLVED] Macro to copy row data based on a condition from several workbooks.
    By Ilikeideas in forum Excel Programming / VBA / Macros
    Replies: 42
    Last Post: 04-01-2013, 04:09 AM

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