如何使用excel比较两列

时间:2014-04-29 12:53:38

标签: excel

如何比较A列和B列如果匹配

A栏

111.111.1.11
111.111.1.12
111.111.1.13
111.111.2.11
111.111.2.12
111.111.3.11

B栏

111.111.2.11
111.111.2.12
111.111.1.12
111.111.1.17
111.111.1.21

预期产出:

111.111.2.11
111.111.2.12
111.111.1.12

1 个答案:

答案 0 :(得分:0)

Start Excel.
Press ALT+F11 to start the Visual Basic editor.
On the Insert menu, click Module.
Enter the following code in a module sheet:



 Sub Find_Matches()
        Dim CompareRange As Variant, x As Variant, y As Variant
        ' Set CompareRange equal to the range to which you will
        ' compare the selection.
        Set CompareRange = Range("C1:C5")
        ' NOTE: If the compare range is located on another workbook
        ' or worksheet, use the following syntax.
        ' Set CompareRange = Workbooks("Book2"). _
        '   Worksheets("Sheet2").Range("C1:C5")
        '
        ' Loop through each cell in the selection and compare it to
        ' each cell in CompareRange.
        For Each x In Selection
            For Each y In CompareRange
                If x = y Then x.Offset(0, 1) = x
            Next y
        Next x
    End Sub


Press ALT+F11 to return to Excel.
Enter the following data (leave column B empty):
A
111.111.1.11
111.111.1.12
111.111.1.13
111.111.2.11
111.111.2.12
111.111.3.11

put B column blank

C
111.111.2.11
111.111.2.12
111.111.1.12
111.111.1.17
111.111.1.21


Select the range A1:A5.
In Excel 2003 and in earlier versions of Excel, point to Macro on the Tools menu, and then click Macros.

In Excel 2007, click the Developer tab, and then click Macro in the Code group.
Click Find_Matches, and then click Run.

您将在b栏中获得预期结果