按行逐行比较Excel中的两列?

时间:2013-06-05 21:12:17

标签: excel vba

目前浏览了论坛,并提出了一个代码来比较两个独立的excel书中的两个列,然后突出显示与CompareRange匹配的任何内容。以下是有关该问题的更多详细信息:

我有两张excel表。每张表中都有这样的数据:

(First Sheet)                             (Second Sheet)
•A         B                             N                O
•7        .7                             3               .56
•6        .6                             8               .45
•5        .5                             9               .55
•4        .4                            11               .2
•3        .3                             8               .22
•2        .2                             9               .55
•1        .1                             8               .54

正如您所看到的,给定此示例,一旦运行宏,就不应突出显示任何内容,因为第一个工作表中的A列或B列中的任何内容都不会直接与列N&列相匹配。 O从第二张纸。问题是,我提出的宏(模块)将突出显示A列中的“3”和来自B列的“.2”,因为它们出现在N列和N列中。 O列分别为。

我想要的是:如果数字“7”和“数字”都只是要强调一个数字。 “。7”在列N和列的同一行中匹配。另一个电子表格中的列O.我不知道该怎么做。为了更准确一点,我举个例子。假设我编辑的数据是这样的。

(First Sheet)                             (Second Sheet)
 •A        B                             N               O
•7        .7                             3               .56
•8        .45                           8               .45
•5        .5                             9               .55
•11        .4                            11               .2
•3        .3                             8               .22
•2        .2                             9               .55
•1        .1                             8               .54

有了这些数据,我想要第二排A& B(“8”和“。45”)突出显示,而A列的错误“3”和B列的“.2”未突出显示。另外,如果A列和A列的第4行,我希望如此。 B(“11”和“.4”)也没有突出显示,只是因为在O中它是.2而在B中它将是.4即使是11的匹配。

请指教。提前谢谢。

附件是我输入的宏/模块,它正确地工作但产生了错误。

而且,(一种较小的问题),具有数据的文件都将具有相同的标题,例如,如果列A和A; N列都有“Dogs”作为第1行和B列的标题。 O都有“Cats”作为第1行的标题。无论如何,宏可以调整,以便比较两个工作簿之间的那两列,而我甚至不必选择或分配范围?非常感谢你。

Sub Find_Matches()
Dim Column1 As Range
Dim Column2 As Range
Set Column1 = Application.InputBox("Select First Column to Compare", Type:=8)
If Column1.Columns.Count > 1 Then
    Do Until Column1.Columns.Count = 1
        MsgBox "You can only select 1 column"
        Set Column1 = Application.InputBox("Select First Column to Compare", Type:=8)
    Loop
End If
    Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
If Column2.Columns.Count > 1 Then
  Do Until Column2.Columns.Count = 1
    MsgBox "You can only select 1 column"
    Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
  Loop
End If
  If Column2.Rows.Count <> Column1.Rows.Count Then
Do Until Column2.Rows.Count = Column1.Rows.Count
  MsgBox "The second column must be the same size as the first"
  Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
Loop


End If
  If Column1.Rows.Count = 65536 Then
    Set Column1 = Range(Column1.Cells(1), Column1.Cells(ActiveSheet.UsedRange.Rows.Count))
    Set Column2 = Range(Column2.Cells(1), Column2.Cells(ActiveSheet.UsedRange.Rows.Count))
  End If
    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 = Workbooks("Book4").Worksheets("Sheet1").Range("N2:N7")
    Set CompareRange1 = Workbooks("Book4").Worksheets("Sheet1").Range("O2:O7")
    ' 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 Column1
        For Each y In CompareRange
            If x = y Then
            x.Interior.Color = vbYellow
            End If


        'x.Offset(0, 5) = x
    Next y
Next x
For Each x In Column2
    For Each y In CompareRange1
        If x = y Then
        x.Interior.Color = vbYellow
        End If
        'x.Offset(0, 5) = x
    Next y
Next x

End Sub

1 个答案:

答案 0 :(得分:1)

将两个循环替换为同时比较两个单元格的循环:

For i = 1 To Column1.Rows.Count
    For j = 1 To compareRange.Rows.Count
        If Column1.Cells(i, 1) = compareRange.Cells(j, 1) Then
            If Column2.Cells(i, 1) = compareRange1.Cells(j, 1) Then
                Column1.Cells(i, 1).Interior.Color = vbYellow
                Column2.Cells(i, 1).Interior.Color = vbYellow
            End If
        End If
    Next j
Next i