Excel VBA-需要比较同一工作表中的两个表。输入表将使用鼠标作为选择

时间:2016-05-11 08:51:56

标签: excel vba excel-vba

我想比较同一张表中的两张表。我将选择第一个表并单击按钮作为第一个输入。同样我会给下一个表输入。当我点击“比较”时按钮,应通过突出显示第二个表中的单元格来显示结果。请帮我这样做。

下面是我用来比较两张表中的两个表的脚本。请帮我找到使用选择选项

提供输入
class Person: NSManagedObject {
    NSManaged var name:String?
    NSManaged var bestFriend:Person?
}

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

Sub CompareTable()

    Dim oldTable As Range, newTable As Range, i As Integer, J As Integer, m As Integer, n As Integer

    On Error Resume Next

    Set oldTable = Application.InputBox(Prompt:="Please Select Raw Data", Title:="Range Select", Type:=8)
    Set newTable = Application.InputBox(Prompt:="Please Select Processed Data", Title:="Range Select", Type:=8)

    i = oldTable.Rows.Count
    J = oldTable.Columns.Count

    For m = 1 To i
        For n = 1 To J
            If oldTable.Cells(m, n) <> newTable.Cells(m, n) Then
                newTable.Cells(m, n).Interior.ColorIndex = 6
            End If
        Next n
    Next m

End Sub

这会问你两个范围。选择要比较的两个表,差异将在第二个表中突出显示。

修改: 的 _________________________________________________________________________________

或者您可以将代码更新为:

Sub CompareBtn_Click()
    Dim first_index As Integer
    Dim last_index As Integer
    Dim rows_index As Integer, columns_index As Integer
    Dim sheet1 As Worksheet
    Dim sheet2 As Worksheet
    Dim r1 As Integer
    Dim r2 As Integer
    Dim found As Boolean
    Dim rng As Range

    Set sheet1 = Worksheets("Source")
    Set sheet2 = Worksheets("Minus")

    rows_index = sheet2.UsedRange.Rows.Count
    columns_index = sheet2.UsedRange.Columns.Count

    For r2 = 1 To rows_index
        For r1 = 1 To columns_index
            If sheet1.Cells(r2, r1) <> sheet2.Cells(r2, r1) Then
                sheet2.Cells(r2, r1).Interior.ColorIndex = 35
            End If
        Next r1
    Next r2
    sheet2.Select
End Sub