Forum Discussion

MicrosoftNewbie121's avatar
MicrosoftNewbie121
Brass Contributor
Jan 30, 2024

MACRO VBA : Find ID in Sheet and Copy DATA from the second Sheet

Hello, could you please help me with this MACRO VBA ? 

i would like to find IDs from Sheet "Updating" and if the same ID exists in Sheet "Update" i would like to copy data corresponding to the ID from "Updating" to "Update" without changing the excel column.

 

Basically copy data from column in sheet Updating to sheet Update "E" to "E", "H" to "F", "K" to "G", from "G" to "H", "I" to "I", "F" to "G"

 

Thank you in advance for your help, in the following post there is the Excel workbook for testing 

3 Replies

  • peiyezhu's avatar
    peiyezhu
    Bronze Contributor

    MicrosoftNewbie121 

    Set Conn = CreateObject("ADODB.Connection")
    Conn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0; HDR=NO;IMEX=3';Data Source=" & ThisWorkbook.FullName
    Set Rst=CreateObject("ADODB.Recordset")

     


    sql="update [Updated$a3:j] as a inner join [updating$a2:j] as b on a.f2=b.f2 set a.f5=b.f5,a.f6=b.f6,a.f7=b.f7,a.f8=b.f8"

    Conn.Execute(sql)
    'sql="select * from [Updated$A3:j]"
    'set Rst=Conn.Execute(sql)
    ' sql="select f2 from [Updating$A2:j]"

    'sql="select f2 from [Updated$A3:j]"
    'Conn.Execute(sql)
    'ActiveCell.CopyFromRecordset Rst

     

      • peiyezhu's avatar
        peiyezhu
        Bronze Contributor
        You are welcome.
        try this way:
        sql="update [Updated$a3:j] as a inner join [updating$a2:j] as b on a.f2=b.f2 set a.f5=b.f5,a.f6=b.f6,a.f7=b.f7,a.f10=b.f8"