简体   繁体   English

使用多个条件在两个工作簿之间复制数据

[英]Copy data between two workbooks using multiple criteria

I am trying to copy data from the Comments column of the Report to the Comments column of the Master. 我正在尝试将数据从“报告”的“注释”列复制到“母版”的“注释”列。 I have tried Vlookup and the If And functions but they have not worked because I am using multiple criteria to identify the data that needs to be copied. 我已经尝试了Vlookup和If And函数,但是它们没有用,因为我使用多个条件来标识需要复制的数据。 I am new to coding but I believe a VBA code is the solution. 我是编码新手,但我相信VBA代码是解决方案。

Someone posted something similar but it doesn't identify the use of multiple criteria. 有人张贴了类似的内容,但没有确定使用多个条件。 This was their post 这是他们的帖子

   Sub Copydata()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim x As Workbook
Dim y As Workbook

Set x = Workbooks.Open("C:\Users\User1\Documents\Workbook1.xlsx")
Set y = Workbooks.Open("C:\Users\User1\Documents\Workbook2.xlsx")
With x.Sheets("Sheet1").UsedRange
    'Now, paste to y worksheet:
    y.Sheets("Sheet2").Range("A1").Resize( _
        .Rows.Count, .Columns.Count) = .Value
End With
x.Close
y.Close SaveChanges:=True
End Sub

Below is an extract of the 2 worksheets in different workbooks 以下是不同工作簿中2个工作表的摘录

Report 报告

Report 报告

Master

Master

A non-VBA solution: In one of the columns on the "Master" tab, concatenate the criteria you're looking up. 非VBA解决方案:在“主”标签上的一列中,将要查找的条件连接起来。 Concatenate the criteria the same way on the "Report" tab. 在“报告”选项卡上以相同的方式连接条件。 Then use a VLOOKUP or INDEX-MATCH looking up by the concatenated criteria. 然后使用VLOOKUP或INDEX-MATCH通过串联条件查找。

Brief Example: 简要示例:

Master

 FNames      LNames   Concat                Comment
 Bill        Smith    =Concatenate(A1,B1)   =VLOOKUP(C1, Report!D1:D2)
 John        Jones    =Concatenate(A1,B1)   =VLOOKUP(C2, Report!D1:D2)

Report 报告

 FNames      LNames   Concat                Comment
 Bill        Smith    =Concatenate(A1,B1)   Bill report submitted
 John        Jones    =Concatenate(A1,B1)   John report not submitted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM