excel vba macro to match cells from two different workbooks and copy and paste accordingly

后端 未结 2 1789
星月不相逢
星月不相逢 2021-01-03 14:37

i have 2 workbooks, workbook A and workbook B. Each workbook has a table. workbook A has 2 columns. All three columns are filled.

  1. product_id
  2. Machine_
2条回答
  •  攒了一身酷
    2021-01-03 15:05

    I tried to replicate your workbooks, I believe they go something like this

    Before Before Click After After Click

    Code changes are minor,

    Sub UpdateW2()
    
        Dim w1 As Worksheet, w2 As Worksheet
        Dim c As Range, FR As Long
    
        Application.ScreenUpdating = False
    
        Set w1 = Workbooks("BookOne.xlsm").Worksheets("Sheet1")
        Set w2 = Workbooks("BookTwo.xlsm").Worksheets("Sheet1")
    
    
        For Each c In w1.Range("D2", w1.Range("D" & Rows.Count).End(xlUp))
            FR = 0
            On Error Resume Next
            FR = Application.Match(c, w2.Columns("A"), 0)
            On Error GoTo 0
            If FR <> 0 Then w2.Range("C" & FR).Value = c.Offset(, -3)
        Next c
        Application.ScreenUpdating = True
    End Sub
    

提交回复
热议问题