Results 1 to 4 of 4

Thread: Copying from one workbook to another

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    5

    Copying from one workbook to another

    Hi everyone,

    new on the forum and getting started with vba:

    I was wondering if someone could help me with the following >>

    I have two Excel files stored on in different folders for example
    test09Feb12.xls is stored in C:\Program Files\blp\data
    report09Feb12.xls is stored in C:\Program Files\blb\data

    Each morning when running the macro I want the macro to go to these folders and copy the content of latest file saved there, or simply the file with the name (with the date of the previous working day).
    The worksheets I want to copy are called for example sheet1 in test09Feb12 and sheet 2 in report09Feb12.

    In want the macro to copy these two sheets to a new Excel file called Summary09Feb12.xls And the name of this file is adapted according to the corresponding date (previous working date).

    After having done this I would like to save the new created Excel file to lets say C:\Program Files\reporting

    After all this please save and close the two source files and keep the Summary file open.



    Like I said, all new to VBA, I've read quite useful codes on the forum which relate to my problem so I hope someone is able to assist me...

    Thanks in advance !!

    VBA noobie

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copying from one workbook to another

    what have you tried so far?
    which parts work?
    which parts give error?
    what does not work correctly?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    5

    Re: Copying from one workbook to another

    Hi,

    thanks for your reaction.

    Well actually I have been trying with the Macro recorder, but not quite there yet. Like I said I'm a rookie.

    I found some code in threat: "Copy data only from multiple workbooks to Workbook with mulitple sheets using Macros"
    but not quite sure how to adapt it to my problem

    I'm looking at it myself, but any tips or help will be greatly appreciated ..

    Cheers

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copying from one workbook to another

    vb Code:
    1. set bk1 = workbooks.open("C:\Program Files\blp\data\test" & format(date - 1, "ddmmmyy") & ".xls")
    2. set bk2 = workbooks.open("C:\Program Files\blp\data\report" & format(date - 1, "ddmmmyy") & ".xls")
    3. set newbk = workbooks.add
    4. bk2.sheets("sheet2").copy newbk.sheets(1)
    5. bk1.sheets("sheet1").copy newbk.sheets(1)
    6. newbk.saveas "C:\Program Files\reporting\Summary" & format(date - 1, "ddmmmyy") & ".xls"
    7. bk1.close false
    8. bk2.close false
    this is untested, will only look at files from the day previous, some modification will be needed to look for files if older, the 2 source files will be closed without changing
    test this and see what modifications are required
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width