X和X匹配的2个工作簿之间的VBA复制值?

我有两个workboks,一个叫奴隶,一个叫主人。

Slave.xlsm

ID Case Size Names 1 1o Michael 2 4 Katie 3 3 Elliot 

Master.xlsm

 ID Case Size Names 1 1o 2 4 3 3 

从从属工作簿,我试图复制名称列中的ID和大小匹配在Master中的值。

我是VBA的新手,所以在网上的一些例子的帮助下,试图编译我自己的代码。 这是我到目前为止:

 Sub GetTheName() Dim s As String, FileName As String s = "C:\Users\******\Documents\*.xlsm" FileName = Dir(s) Do Until FileName = "" If FileName Like "Slave*" Then MsgBox FileName Dim w1 As Worksheet, w2 As Worksheet Dim c As Range, FR As Long Application.ScreenUpdating = False Set w1 = Workbooks.Open(FileName).Sheets(1) Set w2 = ThisWorkbook.Sheets(1) For Each c In w1.Range("C10", w1.Range("C" & 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("R" & FR).Value = c.Offset(, 0) Next c Application.ScreenUpdating = True FileName = Dir() ActiveSheet.Range("A8").Value = Now() Loop End Sub 

如果我删除错误恢复接下来我得到一个types不匹配错误在下面的行:

  FR = Application.Match(c, w2.Columns("R"), 0) 

该代码打开workbok,但不复制任何东西。 我不知道为什么没有被复制。 请有人告诉我我要去哪里错了吗? 谢谢

我已经设法得到你想要的东西…我不知道你是否会对我的答案感兴趣,但它做你想要的…

  1. 首先添加一个列,在此连接从属页面中的A和B列
  2. 使用INDEX – MATCH方法查找匹配项

我在D列上添加了连接列…所以公式会是这样的…

  =INDEX(SLAVE!C2:C4;MATCH(CONCATENATE(MASTER!A2;MASTER!B2);SLAVE!D2:D4;0)) 

这是VBA代码

 Sub GetNames() ' ' GetNames Macro ' ' LastRow = Sheets("SLAVE").Cells(Rows.Count, 1).End(xlUp).Row Sheets("SLAVE").Activate Sheets("SLAVE").Range("D2").FormulaR1C1 = "=CONCATENATE(RC[-3],RC[-2])" Sheets("SLAVE").Range("D2").AutoFill Destination:=Range("D2:D" & LastRow & ""), Type:=xlFillDefault LastRow = Sheets("MASTER").Cells(Rows.Count, 1).End(xlUp).Row Sheets("MASTER").Activate Sheets("MASTER").Range("C2").FormulaR1C1 = _ "=INDEX(SLAVE!RC:R[2]C,MATCH(CONCATENATE(MASTER!RC[-2],MASTER!RC[-1]),SLAVE!RC[1]:R[2]C[1],0))" Sheets("MASTER").Range("C2").AutoFill Destination:=Range("C2:C" & LastRow & ""), Type:=xlFillDefault End Sub 

基于您的评论中的types不匹配,我会指出你在这里:

Application.Match给出types不匹配

你可能没有在规定的范围内罚款。

你可以使用AutoFilter()

 Option Explicit Sub main() Dim cell As Range, masterRng As Range With Sheets("Master") '<--| reference your "Master" sheet Set masterRng = .Range("A2", .Cells(.Rows.count, 1).End(xlUp)) '<--| reference its columns A cells from row 2 down to last not empty row End With With Sheets("Slave") '<--| reference your "Slave" sheet With .Range("B1", .Cells(.Rows.count, 1).End(xlUp)) '<--| reference its columns A and B from row 1 (headers) down to column A last not empty row For Each cell In masterRng '<--| loop through "Master" sheet column A ID Size" .AutoFilter field:=1, Criteria1:=cell.Value '<--| filter it on its 2nd column (ie column B) with current cell offset 1 column value (ie current "Master" sheet "Case Size") .AutoFilter field:=2, Criteria1:=cell.Offset(, 1).Value '<--| filter it on its 2nd column (ie column B) with current cell offset 1 column value (ie current "Master" sheet "Case Size") If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then '<--| if any cell filtered other than headers cell.Offset(, 2) = .Resize(.Rows.count - 1, 1).Offset(1, 2).SpecialCells(xlCellTypeVisible).Cells(1, 1) '<--|write first filtered 3rd column cell value in current cell offset 2 columns value (ie current "Master" sheet "Names") End If Next cell End With .AutoFilterMode = False End With End Sub