2

目前浏览了论坛,并提出了一个代码来比较两本单独的 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 列和 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 列和 B 列(“11”和“.4”)的第 4 行根本没有突出显示,我希望它,只是因为在 O 中它是 0.2,而在 B 中它会是 0.4,即使11的比赛。

请指教。提前致谢。

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

而且,(有​​点小问题),两个带有数据的文件都将具有相同的标题,例如,如果 A 列和 N 列都具有“Dogs”,因为它在第 1 行的标题和 B 列和 O 都具有“猫”作为它在第 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

结束子

4

1 回答 1

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
于 2013-06-06T13:45:00.197 回答