Excel工作表比较

时间:2019-01-31 10:21:30

标签: excel excel-formula

我在Excel中进行比较和替换值时遇到困难。如果有人可以帮助我并指导我,那将是很好的。

两张纸上都没有票。我想比较票证编号,如果它们匹配,则将类型和子类型从sheet1复制到sheet2类型和子类型列。我附上图片供您参考

enter image description here

2 个答案:

答案 0 :(得分:0)

如果您想使用VBA,这可以为您提供帮助:

Option Explicit

Sub test()

    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim ws1LastRow As Long, i As Long, ws2LastRow As Long, y As Long
    Dim ws1TicketNo As String, ws2TicketNo As String, ws1Type As String, ws2Type As String, ws1SubT As String, ws2SubT As String

    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2")

    ws1LastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
    ws2LastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row

    For i = 2 To ws1LastRow

        ws1TicketNo = ws1.Range("A" & i).Value
        ws1Type = ws1.Range("B" & i).Value
        ws1SubT = ws1.Range("C" & i).Value

            For y = 2 To ws2LastRow

                ws2TicketNo = ws2.Range("A" & i).Value
                ws2Type = ws2.Range("B" & i).Value
                ws2SubT = ws2.Range("C" & i).Value

                If ws1TicketNo = ws2TicketNo Then
                    ws2.Range("B" & i).Value = ws1Type
                    ws2.Range("C" & i).Value = ws1SubT
                    Exit For
                End If

            Next y

    Next i

End Sub

工作表1:

enter image description here

表格2:

enter image description here

结果:

enter image description here

答案 1 :(得分:0)

您可以使用VLOOKUP function

使用该公式,您可以从sheet2中搜索每个票证编号,在sheet1中搜索信息,并返回所需的值(类型或子类型)。

=VLOOKUP(A2;Sheet1!$A$2:$B$4;2;FALSE)之类的东西应该对您有用。使其适应您的需求