+ Reply to Thread
Results 1 to 7 of 7

copy data from one workbook to another

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    Talking copy data from one workbook to another

    hi,

    can anyone help me how to create a macro in copying data from one workbook to another? I know how to link the workbook so that it can update the other workbook but I would like to know if it is possible to simply automatically copy one work book to another...I attached a sample workbooks. Book1 contains the data to be copied on Book2. please help me in copying the start and end time on book 1 going to book 2 once book2 is opened (assuming that book1 is opened as well). Appreciate any response..

    Regards,

    Stoey
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: copy data from one workbook to another

    Hello stoey,

    Place this macro in a VBA module in "Book2.xls". There is no error checking to see if "Book1.xls" is open. Change the workbook and worksheet names accordingly.
    Sub CopyTimes()
    
      Dim SrcRng As Range
      Dim DstRng As Range
      
        Set DstRng = ThisWorkbook.Worksheets("Day3").Range("L10:K33")
        Set SrcRng = Workbooks("Book1.xls").Worksheets("Sheet1").Range("L10:K33")
        
          DstRng.Value = SrcRng.Value
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    Re: copy data from one workbook to another

    hi Leith,

    I tried the code and it works when 2 workbooks are opened. I tried adding another worksheet on book1 and I made another workbook named book3 and placed the code below on book2.

    Private Sub Workbook_Open()
      Dim SrcRng, Book3 As Range
      Dim DstRng As Range
      
      
      
        Set DstRng = ThisWorkbook.Worksheets("Day3").Range("L10:K33,O10:P33")
    
        Set SrcRng = Workbooks("Book1.xls").Worksheets("Sheet1").Range("L10:K33")
        Set SrcRng = Workbooks("Book1.xls").Worksheets("Sheet2").Range("L10:K33")
        
        Set DstRng = ThisWorkbook.Worksheets("Day4").Range("L10:K33")
        Set Book3 = Workbooks("Book3.xls").Worksheets("Sheet1").Range("L10:K33")
        
          DstRng.Value = SrcRng.Value
          DstRng.Value = Book3.Value
          
    End Sub
    however when i tried opening book 2 (assuming book 1 and book 3 are opened) the data from book 3 are the only ones that were copied. the data on book 1 were not... any ideas why this happened? I attached sample files again...


    Regards,

    Stoey
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    Re: copy data from one workbook to another

    hi Leith,

    any idea why the code i tried only copies book 3 data?

    regards,

    stoey

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: copy data from one workbook to another

    Hello stoey,

    I provided you a solution for 2 workbooks. Now, you present a problem with 3 workbooks. Explain to me what it is you want to do and I can provide you with the correct macro.

  6. #6
    Forum Contributor
    Join Date
    08-07-2008
    Location
    United States
    MS-Off Ver
    Microsoft Office 2003
    Posts
    362

    Re: copy data from one workbook to another

    hi again Leith,

    I apprecaite and thank you for the correct macro you provided earlier...actually what I am currently doing is I have 20 workbooks which I would like some specific data to be copied on one workbook. I attached sample workbooks before which is book1 and book2. Since you provided me a working code, I tried tweaking it and trying to learn about it so that I could apply it on the current program I am working on...


    regards,

    stoey

  7. #7
    Registered User
    Join Date
    06-20-2011
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: copy data from one workbook to another

    Hi Leith Ross,

    Thank U so much 4ur helpful tips..

    //reddy

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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