Copy Data Range From 1 Workbook To Another

  • Hi there.


    Can anyone please help me with the following exercise?


    I need to copy the same data range from Sheet 1 of different workbooks in the same subdir to output.xls (also in the same subdir).


    In workbook output.xls, I've defined the range of workbooks (source files) as name filenames. I need to copy the same data range (say B8:R8) from each source file to the cells adjacent to the filenames in output.xls.


    Eg. I have defined the range of files to copy data from as filenames in Col A of Sheet 1 in output.xls.



    Col A
    sales_10-09-06
    sales_10-10-06
    sales_10-11-06


    Workbook sales_10-09-06 contain sales data of various product to different firms as below:


    Col A Col B Col C .................................. Col R
    prod A prod B prod q
    Firm 1 50 20 100
    Firm 2 20 25 80
    Firm 3 30 50 20


    How do I copy B8:R8 of sales recorded for each day in sales_date.xls to Col B of Sheet 1 in output.xls? There are 200 files (specified by the name filenames in output.xls) that I need to copy B8:R8 from.


    Urgent help is needed and appreciated.



    Thanks in advance.



    Regards,
    Nadia

  • Re: Copy Data Range From 1 Workbook To Another


    There is a correction to the previous post:


    The data range B8:R8 from each source file will need to be copied to Col B to R next to source workbook name in output.xls.


    Eg, In output.xls (Sheet 1):


    col A col B.......................... col R
    sales_10-09-06 50 20
    sales_10-10-06 70 35
    sales_10-11-06 30 20


    I hope this will make things clearer.



    Thanks.

  • Re: Copy Data Range From 1 Workbook To Another


    hi mutant,


    adapt this code to your solution:


    hope this helps

    Cheers
    ___________
    Xlite :sheep:
    [st]All you need to learn VBA is an internet connection and Ozgrid.com[/st]

  • Re: Copy Data Range From 1 Workbook To Another


    Xlite,


    Thanks for the codes, but I don't really know how to adapt the codes. I am an accountant and no programmer at all. :) In fact, I am a beginner who is trying to pick up vba by myself recently. I just need the data to perform my analysis.


    Have I missed something? I don't see any code segment which involves pasting the data range copied from the source workbooks to columns next to A on sheet 1 in output.xls.


    Please help me if you can. :confused:



    Thanks very much.



    Regards,
    Nadia

  • Re: Copy Data Range From 1 Workbook To Another


    hi Nadia,


    i have put comments on most important parts of the code,
    have a look and ask me again which part you are not sure of.
    btw, place this code in a normal module;
    while yr output file is open click Alt+F11,
    go to insert module
    and paste the code there



    this code copies the range:

    Code
    'copy range to the last available row of the output file
    Workbooks(x(i)).Sheets("Sheet1").Range("B8:R8").Copy ThisWorkbook.Sheets(1).Range("A65536").End(xlUp).Offset(0, 1) 
    'the offset means ofsetting 1 column from col A, ie. col B
    'column A cointains the filenames from which the datas came from


    also

    Code
    'change this:
    ThisWorkbook.Sheets(1) 
    'to:
    ThisWorkbook.Sheets("YourSheetName")
    'if your default sheet is not sheet 1



    you need not tamper with the Function, just costumize the above module. :)


    feel free to ask :)

    Cheers
    ___________
    Xlite :sheep:
    [st]All you need to learn VBA is an internet connection and Ozgrid.com[/st]

  • Re: Copy Data Range From 1 Workbook To Another


    Xlite,


    Thanks soooooooooooooooooooooooo much. I truly appreciate your help not only in the coding but also in the comments. The comments have helped me to understand what each line of the code does.


    I haven't tried out the codes yet as I will need to get back to the office to do that.


    Your offer to help & explain is just something which makes my day & possibly many other newbies' in the forum. I'm rather surprised & impressed with the number of helpful people like yourself in the forum.


    Keep up the good work, Xlite!



    Thanks again.



    Cheers,
    Nadia

  • Re: Copy Data Range From 1 Workbook To Another


    Dear Xlite,


    I'm TQV1969 that you helped me in Sat. I've got the same situation and I've tried your code but it showed the error message "Compile error: Sub or Funtion not defined" and highlight yellow backgroud for "GetFileList" in your code. Is it one other code for that or excel's funtion originally?


    Thanks.
    TQV1969

  • Re: Copy Data Range From 1 Workbook To Another


    hi tqv1969,


    you have to use the code posted in my first post on this thread, it has a
    "GetFileList" Function.


    i assume you took a 'partial' code which i used to explain to Mutant? :)

    Cheers
    ___________
    Xlite :sheep:
    [st]All you need to learn VBA is an internet connection and Ozgrid.com[/st]

  • Re: Copy Data Range From 1 Workbook To Another


    Xlite,


    I hope you are keeping well. I've tried the 2 segments of the codes u posted on this thread. Unfortunately, it says "no matching file" when I ran it. I've defined FileSpec in my destination workbook which contains the macro.


    I don't see any reason why there is no matching files. Can you please help me to look at/test out the codes again?


    Do I need to define or do anything about the following:
    1. FileSpec
    2. Lbound and Ubound
    3. FileArray
    4. Filename (Does this name define the range of file to look for in the specified dir?)


    Please help.



    Thanks.



    Cheers,
    Nadia

  • Re: Copy Data Range From 1 Workbook To Another


    Xlite,


    You can ignore the message I just posted. It was a silly mistake I made. It is working!!



    Thanks veeeeeeeeeeeeery much for the codes.



    Cheers,
    Nadia

  • Re: Copy Data Range From 1 Workbook To Another


    Dear Xlite,


    Yes, I took the second code only. I will try te first one. Thanks for your quick response.


    Best regards,
    TQV1969

  • Re: Copy Data Range From 1 Workbook To Another


    Dear mutant /Xlite,


    I've tried all code and modify to correct directory and sheet name but it showed "no matching file". Mutant: could you share how you fix it?
    Thanks.


    TQV1969

  • Re: Copy Data Range From 1 Workbook To Another


    hi tqv1969,


    the only reason i can think of is the folder path not specified correctly.
    remember to put the "\" after the folder name...


    let me know if still not working :)

    Cheers
    ___________
    Xlite :sheep:
    [st]All you need to learn VBA is an internet connection and Ozgrid.com[/st]

  • Re: Copy Data Range From 1 Workbook To Another


    Dear xlite,


    I've tried with my dir as C:\Copy\ and change to Sheet2 for those Sheet(1) in your code. And it showed message again.
    I've got the source file with data in Sheet2 and want to copy all sheet contents from each of them to final file in Sheet2 also.
    Did I do some thing wrong?:crying:


    Best regards,
    TQV1969

  • Re: Copy Data Range From 1 Workbook To Another


    Dear xlite,


    It worked now b/c I changed dir name. I'm so.....but I dont know how to select all data cells in my Sheet2. Please help!


    Best regards,
    TQV1969

  • Re: Copy Data Range From 1 Workbook To Another


    Dear xlite,


    I can select all sheet now but one more issue is the second set of data didn't connect from the last row in out put file but ONE row right after that. So the second set of data overwrite on the previous one but leave first row left only and so on for those next files.
    I'm sorry to border you so many times but I tried my best but It couldn't.:crying:


    Best regards,
    TQV1969

  • Re: Copy Data Range From 1 Workbook To Another


    hi tqv1969,


    this code extracted from above pastes the data one row below the last row:

    Code
    Workbooks(x(i)).Sheets("Sheet1").Range("B8:R8").Copy ThisWorkbook.Sheets(1).Range("A65536").End(xlUp).Offset(0, 1) 'copy range to the last available row of the output file


    can you show us yr code so we can analyse it?
    thx

    Cheers
    ___________
    Xlite :sheep:
    [st]All you need to learn VBA is an internet connection and Ozgrid.com[/st]

  • Re: Copy Data Range From 1 Workbook To Another


    Dear xlite,


    I've attached here the two workbooks with data I want to copy to Test.XLS and data from Sheet2 for all three files. The modified code is in Test.xls file.
    You can see the result of the code in Sheet2 in Test.xls.
    Btw,There are any issues if i copy different sheet name from different files to Test.xls and in that case what the code should be.


    Many thanks again for your time with me:) I hope this time it work!
    Best regards,
    TQV1969

  • Re: Copy Data Range From 1 Workbook To Another


    hi TQV1969,


    i cleaned up yr source files coz of these few points:


    1. you select from A5 although data start from A1,
    so i use from A1


    2. you have a non-data causing a blank row incl in the selection,
    so i deleted the non-data.


    3. you have intermittent merged cells in col A which may casue problems,
    so i replaced them with ordinary cells.


    i've got it working based on the cleaned source files, as they always say:
    a good housekeeping keeps all the (excel) bugs away! :)


    hope this helps

  • Re: Copy Data Range From 1 Workbook To Another


    Dear xlite,


    I've tested your new files and it worked. Many thanks Xlite.
    But the issue to me that is I've got one macro to edit the raw sheet to clean one after I bring all raw sheets(Sheet2 in this case) orginally to one sheet.
    That means I need to edit all of them one by one first and apply your code later to bring all reparate sheets to test.xls later. If that is the case, I must edit one by one manually before apply your code.
    Do you have any other way to do the same as current code but bring original Sheet2 content to Test.xls?


    I understand that the Good House Keeping will help but I can't avoid that b/c the resources file are big and a lot. Sorry I ask you again.


    Best regards,
    TQV1969

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!