Results 1 to 14 of 14

Copy values from one woorkbook to another according to cell location

Threaded View

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

    Copy values from one woorkbook to another according to cell location

    Hello!

    Currently what my code does is that it copies the corresponding values "value 1", "value 2" and "value 3" in regards to the respective "ID" from WorkbookA. It then pastes the values to WorkbookB in the corresponding Sheets. The code compares the ID from WorkbookA with the IDs from WorkbookBs sheets. When the ID fits it pastes the values into the respective cells. It then proceeds to the next ID (see attachments).

    What I want my code to do instead, is still to copy the values from WorkbookA into their respective places in the corresponding sheet. But the values should now be pasted into 1 column, instead of 3 (see attachment: WorksheetC). The cells for which the numbers should be copied to, are always G10, G16 and G19 for all the "Test person" sheets.

    An example: The values 5, 8 and 12 for IG1 in WorkbookA should be pasted into the cells G10, G16 and G19 respectively in WorkbookC, under sheet "Test person 1", as the IDs correspond (thus replacing the values 32, 44 and 22).

    Is there a way for the code to paste the values into their respective cells, just based on cell position?


    CODE:

    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
    Thanks!
    Attached Files Attached Files
    Last edited by Allerdrengen; 08-01-2017 at 09:06 AM. Reason: Forgot to attach code

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA Code Modification
    By guapo in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-12-2014, 08:54 AM
  2. help with Code modification
    By onp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2014, 07:00 PM
  3. Little modification in code
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2013, 02:15 PM
  4. code modification
    By tofimoon4 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-12-2010, 06:26 AM
  5. Code Modification to alert 100% and over
    By Preatorian in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-20-2009, 03:23 AM
  6. Modification to existing Code
    By Jim May in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-04-2006, 12:55 PM
  7. [SOLVED] modification for the code
    By srinivasan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-20-2005, 11:05 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