比较两张纸

时间:2018-12-27 05:11:16

标签: excel-formula

这是MS Excel公式问题:

我想比较两个电子表格的客户名称和汽车型号。我可以将每月数据导入到新表中。我想将Sheet1!ColumnA(客户名称)和Sheet1!ColumnB(汽车模型)与Sheet2!ColumnA(客户名称)和Sheet2!ColumnB(汽车模型)进行比较。当两个变量都匹配时,从Column3(date)中的Sheet2返回相应的单元格。如果不匹配,请留空。

我相信这是一连串的逻辑函数,但是我尝试了至少20个,但没有成功。预先感谢!

1 个答案:

答案 0 :(得分:0)

Sheet1:

enter image description here

输出:

enter image description here

尝试:

Option Explicit

    Sub test()

        Dim i As Long, j As Long
        Dim LastrowS1 As Long, LastrowS2 As Long
        Dim Customer_NameS1 As String, Car_ModelS1 As String, Customer_NameS2 As String, Car_ModelS2 As String
        Dim DateS1 As Date

        LastrowS1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
        LastrowS2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row

        For i = 2 To LastrowS1
            With ThisWorkbook.Worksheets("Sheet1")
                Customer_NameS1 = .Range("A" & i).Value
                Car_ModelS1 = .Range("B" & i).Value
                DateS1 = .Range("C" & i).Value
            End With
                For j = 2 To LastrowS2
                    With ThisWorkbook.Worksheets("Sheet2")
                        Customer_NameS2 = .Range("A" & j).Value
                        Car_ModelS2 = .Range("B" & j).Value
                    End With

                    If Customer_NameS1 = Customer_NameS2 And Car_ModelS1 = Car_ModelS2 Then
                        Sheet2.Range("C" & j).Value = DateS1
                        Exit For
                    End If

                Next j

            Next i

    End Sub