+ Reply to Thread
Results 1 to 3 of 3

copy data from a sheet in one workbook to a sheet with the same name in another workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2013
    Location
    Pretoria
    MS-Off Ver
    Excel Latest Version
    Posts
    11

    copy data from a sheet in one workbook to a sheet with the same name in another workbook

    Dear all,

    I created a macro that open a second workbook when I open the first workbook. Both contains a "Contacts" worksheet that I reference in a few places. Both contact list are needed but I only maintain the one in the first workbook and then want to update the same sheet in the second workbook everytime I start the first one.
    At first I simply deleted the sheet in the second workbook; " WBOpen1.Sheets("Contacts").Delete " and then copy the same sheet from the fist workbook;
    " WBOpen.Sheets("Contacts").Copy After:=WBOpen1.Sheets(10) " . This works fine but then all the formulas in the second workbook that reference this worksheet loses it reference;
    for instance this formula " =IFERROR(VLOOKUP(D6,Contacts!$B$4:$G$278,5,FALSE),"------") " becomes this " =IFERROR(VLOOKUP(D6,#REF!,5,FALSE),"------") "

    Any suggestion will be highly appreciated

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: copy data from a sheet in one workbook to a sheet with the same name in another workbo

    When you delete a sheet all formulas pointing at that sheet goes in error... you could try to clear data and copy new data without delete the sheet.
    Try to change your code with:
    WBOpen1.Sheets("Contacts").Range("1:" & Rows.Count).Delete 
    WBOpen.Sheets("Contacts").Range("1:" & Rows.Count).Copy WBOpen1.Sheets("Contacts").Range("A1")
    Regards,
    Antonio

  3. #3
    Registered User
    Join Date
    02-06-2013
    Location
    Pretoria
    MS-Off Ver
    Excel Latest Version
    Posts
    11

    Re: copy data from a sheet in one workbook to a sheet with the same name in another workbo

    Hi Antonio
    thanx for that, it looks neat and will definately work, in fact I'm going to tr it as well. I did however tried with the code below and it also seems to solve my problem.

    Sub Update_Contacts()
    
    Dim Filename1 As String
    Dim OrigSheet, DestSheet As Worksheet
    Dim WBOpen As Workbook
    Dim WBOpen1 As Workbook
    
     Filename1 = Sheets("Admin").Range("E4").Value & ".xlsm"
    
        Set WBOpen  = ThisWorkbook
        Set WBOpen1 = Workbooks(Filename1)
    
      Set OrigSheet = WBOpen.Worksheets("Contacts")
      Set DestSheet = WBOpen1.Worksheets("Contacts")
      OrigSheet.Range("B4:G500").Copy
      DestSheet.Range("B4:G500").PasteSpecial
      WBOpen1.Sheets("Contacts").Protect
    
    End Sub

+ 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