+ Reply to Thread
Results 1 to 16 of 16

Macro to find data in source workbook and copy paste to target workbook

  1. #1
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    186

    Macro to find data in source workbook and copy paste to target workbook

    Dear All,

    I have a source workbook from which I will copy sales data from onto a template workbook to create a sales analysis. The source workbook has dropdown menus at the top of each column which allow me to filter out the blocks of data I need.

    How hard would it be to write a macro that can do this for me?

    I have attached an example of the source and target workbooks.

    For the purpose of an example, lets as I want the macro to find all the lines in the source workbook that read "AREA 1" in column A, "SALES" in column D, and "2013 in column F. Having identified those lines I then want the data in columns B & C copies across to the target workbook entitled 'Sales Analysis Template' and pasted onto the '2013' sheet in columns A & B. And then the figures in columns G-R on the source workbook copied across to the 2013 sheet on the target workbook, and pasted into columns C-N (i.e. the sales figures for January to December copied across and pasted in the relevant columns).

    I then need to repeat the process for lines that match "2014" in column F of the source workbook and copy the same information across but pasted into the respective columns in the target workbook, this time on the 2014 sheet, the column layouts are the same as with the 2013 sheet.

    Finally I would like to then identify how many blank lines are left over on the 2014 sheet and delete them all the way down to line 1665, retaining the summary section that exists on the template below this.

    This is a simplified example of what I am attempting to do, I am confident that I will be able to adapt appropriate examples to fit the real data, and hopefully learn something in the process.

    If anyone fancies having a go at helping me out with this, I would be most grateful.

    Many thanks,

    Dave.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to find data in source workbook and copy paste to target workbook

    Maybe:

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    186

    Re: Macro to find data in source workbook and copy paste to target workbook

    Hi,

    Thanks for this, it's a good start. This puts the correct data into the correct place on the 2013 sheet, but not the 2014 sheet for some reason.

    I need for the 2014 figures for those same customers to also be pasted onto the 2014 sheet, so that we end up with the same customer's figures on both sheets and on the same rows, only difference is which year the figures are taken from.

    Also this deleted the part of the 2014 sheet that I actually want to retain. It's all the blank rows left over above row 1665 that need to be deleted, while retaining what is below line 1665. So after the figures are pasted onto the 2014 sheet, delete rows from that sheet starting 1 row below the pasted data, going down to row 1665. Do not delete rows below 1665.

    Trying to adapt the code but I'm not very experienced at this yet.

    Any further help would be appreciated.

    Many thanks,

    Dave.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to find data in source workbook and copy paste to target workbook

    See if this works any better.

    Please Login or Register  to view this content.
    I think you may have merged cell in Sheet 2014 which may cause problems. Is that the case?

  5. #5
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    186

    Re: Macro to find data in source workbook and copy paste to target workbook

    It was not my intention to merge cells, I have checked though and am pretty sure there are none but it does have various formulas and conditional formatting already in place if that matters, it was created as a template to manually paste this data onto.

    This script does now retain the bottom section of the 2014 sheet as I described. It still doesn't paste the data correctly onto the 2014 sheet.

    There is something else I realised I haven't mentioned yet. Ideally this needs to consider what is the current month, and copy the 2014 figures upto the current month, but not including the current month, and for this to work correctly at whatever time of the year it is used. Is that possible?

    I have attached a further example that shows what it ideally should look like after running the macro, this is assuming it is used now with regards to the point I have made above.

    When doing this, I pasted the values only onto the template, maybe that is important as well.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to find data in source workbook and copy paste to target workbook

    When tested, it looked like it pasted into Columns AB of 2014 correctly, but not into C. If that is your finding let me know.

  7. #7
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    186

    Re: Macro to find data in source workbook and copy paste to target workbook

    Yes it did copy the data into columns A&B of the 2014 sheet but it was underneath the summary section at the bottom, rather than being positioned on the same rows as on the 2013 sheet, as if the pasting position starts from bellow the last rows to have data already in them.

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to find data in source workbook and copy paste to target workbook

    I thought that was what you wanted? Did you want to paste over the data in 2014?

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to find data in source workbook and copy paste to target workbook

    OK. Now I think I understand, try:

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    186

    Re: Macro to find data in source workbook and copy paste to target workbook

    Yes this copies the data into the correct places and deletes left over rows, that is exactly what I meant. This is a very helpful example for getting me started off.

    One last thing on this one, how can we paste the data as if we were choosing 'paste values' when doing it manually in Excel, so that it doesn't paste over my cell formatting on the template?

  11. #11
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to find data in source workbook and copy paste to target workbook

    Perhaps:

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    186

    Re: Macro to find data in source workbook and copy paste to target workbook

    This line returns run-time error 1004, object-defined or application-defined error

    Workbooks("Sales Analysis Template.xlsm").Sheets("2013").Range(Cells(x, "A"), Cells(x, "B")).Value = Range(Cells(i, "B"), Cells(i, "C")).Value

  13. #13
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to find data in source workbook and copy paste to target workbook

    Try this:

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    186

    Re: Macro to find data in source workbook and copy paste to target workbook

    Very helpful indeed, this does exactly what I meant and is useful learning material for me. I have adapted it to work with my actual data layout rather than the example I posted and got it working fine. There are other issues that I will need to ask for help with but I will post the next one in a separate thread that focus' on that particular issue, it will be to do with excluding the months of the year that haven't occurred yet when copying sales data to the 2014 sheet. Since this thread was about finding and copying data from a source workbook to a target one, you have completely answered the question that I posted in this threat and I feel I should mark it as solved. Again thanks very much for your help.

    Best regards,

  15. #15
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    186

    Re: Macro to find data in source workbook and copy paste to target workbook

    Post deleted

  16. #16
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to find data in source workbook and copy paste to target workbook

    You're welcome. Glad to help out and thanks for the feedback.

+ 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. Macro To copy data of each month from source to target
    By jonhdoe1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2013, 08:02 AM
  2. copy text from a source workbook to target workbook
    By JMJ123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2013, 10:24 AM
  3. [SOLVED] Copy & paste source worksheet into exsiting tab in another workbook
    By Webbers in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-01-2012, 08:48 AM
  4. Replies: 3
    Last Post: 04-24-2012, 11:56 AM
  5. copying data from source workbook to target workbook by headers
    By tweety127 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-25-2009, 11:35 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