+ Reply to Thread
Results 1 to 5 of 5

Moving data from one workbook to another

Hybrid View

  1. #1
    Registered User
    Join Date
    04-03-2017
    Location
    Denmark
    MS-Off Ver
    365
    Posts
    94

    Post Moving data from one workbook to another

    I have two workbooks: WorkbookA and WorkbookB. (Attached)
    I want to copy the values for the person with ID number: IG1 from workbookA to the corresponding sheet in workbookB. The sheet for IG1 would be sheet "Test person 1" as cell B1 in this sheet, contains the corresponding ID (IG1).
    There are already values in the cells (32, 7 and 1) in WorkbookB (sheet: test person 1), these values should be replaced by 5, 8 and 12 (from WorkbookA), respectively.

    This procedure should then continue for the next ID (IG2), and so forth. If the ID is not found in workbookB, it should skip it, and go to the next ID, until there are no more IDs.

    So i need a code that compares the ID from column A in WorkbookA, with its corresponding ID, which is located in one of the "Test person" sheets in workbookB, in cell B1. It should then copy and paste the corresponding values (value 1, value 2 and value 3) that matches the ID, from workbookA, to the cells D2, E2 and F2 in workbookB.

    Thank you very much!
    Attached Files Attached Files
    Last edited by Allerdrengen; 07-13-2017 at 07:03 AM.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Moving data from one workbook to another

    I'd use ADO.

    Sub Demo()
    Dim cn As Object, rst As Object
    Dim strQuery As String
    Dim ws As Worksheet
    
    Set cn = CreateObject("ADODB.Connection")
    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.Path & "\WorkbookA.xlsx;" & _
                            "Extended Properties=""Excel 12.0 Xml;HDR=Yes:"""
        .CursorLocation = 3
        .Open
    End With
    
    For Each ws In ThisWorkbook.Worksheets
        strQuery = "SELECT [Value 1], [Value 2], [Value 3] FROM [Sheet1$] WHERE [ID:] = """ & ws.[B1].Value & """"
    
        Set rst = CreateObject("ADODB.Recordset")
        rst.Open strQuery, cn, 1, 3
        
        If rst.RecordCount > 0 Then
            ws.Range("D2").CopyFromRecordset rst
        End If
    Next
    
    rst.Close
    cn.Close
    
    End Sub
    Save both attached to same folder. And while WorkbookA is closed, run code in WorkbookB.
    Attached Files Attached Files
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    04-03-2017
    Location
    Denmark
    MS-Off Ver
    365
    Posts
    94

    Re: Moving data from one workbook to another

    Thank you! It works perfectly.

  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: Moving data from one workbook to another

    Maybe:

    Sub Allerdrengen()
    Dim i As Long, x As Range, y
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    With Workbooks("workbookb.xlsx")
    ReDim y(1 To .Sheets.Count)
        For i = LBound(y) To UBound(y)
            Set x = Workbooks("workbooka.xlsx").Sheets("Sheet1").Columns(1).Find(.Sheets(i).Cells(1, 2), LookIn:=xlValues, lookat:=xlWhole)
                If Not x Is Nothing Then
                    .Sheets(i).Range("D2").Resize(, 3).Value = x.Offset(, 1).Resize(, 3).Value
                End If
            Set x = Nothing
        Next i
    End With
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    End Sub
    Last edited by JOHN H. DAVIS; 07-12-2017 at 10:56 AM.

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

    Re: Moving data from one workbook to another

    You're welcome. Glad to help out and thanks for the feedback and rep. Please mark this thread as SOLVED.

+ 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. Moving data between worksheets within the same workbook
    By CountryPenguin in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-26-2015, 12:41 PM
  2. Moving Data from One Workbook to Another Workbook
    By abutler911 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-16-2013, 04:10 PM
  3. Moving data between two workbook with one a variable name
    By Scans in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-10-2012, 07:35 PM
  4. Replies: 4
    Last Post: 09-15-2012, 02:18 PM
  5. Moving data to a closed workbook
    By bouncey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-29-2010, 10:31 PM
  6. Moving Changing data to new tab in workbook
    By Lime in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-23-2005, 11:40 AM

Tags for this Thread

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