比较两组文字,如果有差异则套用

时间:2019-01-11 20:31:19

标签: excel vba

我正在比较两组文本(2个不同的单元格),如果它们不匹配,那么我想将新数据作为新行显示。

我有一个现有的代码,可以很好地比较两组不同的数据,如果它有所不同(通常是数字,日期或带有字母的数字,即ABC456),那么它将覆盖新的数据行并将其作为新的数据插入排到我的工作表中。但是,我现在要比较一个文本线程(几个单词,数字,符号),以查看它是否与该行之前的内容有所不同,以及是否与之不同,然后将其拖延。我需要为我的代码中的And .Cells(i, 12) = laws.Cells(j, 12)部分做些不同的事情吗?谢谢。

        Sub CompareSheets()

        Application.ScreenUpdating = False
        Application.DisplayAlerts = False

            Dim laws As Worksheet
            Set laws = Sheets("LOB")
            Dim interiorws As Worksheet
            Set interiorws = Sheets("copy")

            Dim RowsMaster As Integer, Rows2 As Integer
            RowsMaster = laws.Cells(1048576, 1).End(xlUp).Row
            Rows2 = interiorws.Cells(1048576, 1).End(xlUp).Row
            ' Get the number of used rows for each sheet


            With Worksheets("copy")
         For i = 2 To Rows2
                ' Loop through Sheet 2
                    For j = 2 To RowsMaster
                    ' Loop through the Master sheet
                        If .Cells(i, 3) = laws.Cells(j, 3) And .Cells(i, 5) = laws.Cells(j, 5) And .Cells(i, 6) = laws.Cells(j, 6) And .Cells(i, 7) = laws.Cells(j, 7) And .Cells(i, 12) = laws.Cells(j, 12) Then
                        ' If a match is found:
                            laws.Cells(j, 5) = .Cells(i, 5)
                            ' Copy in contact info
                            Exit For
                            ' No point in continuing the search for that company
                        ElseIf j = RowsMaster Then
                        ' If we got to the end of the Master sheet
                        ' and haven't found a company match
                            RowsMaster = RowsMaster + 1
                            ' Increment the number of rows
                            For k = 1 To 12 ' Change 3 to however many fields Sheet2 has
                                laws.Cells(RowsMaster, k) = .Cells(i, k)
                                laws.Range(laws.Cells(j + 1, 1), laws.Cells(j + 1, 11)).Interior.ColorIndex = 24
                                ' Copy the data from Sheet2 in on the bottom row of Master
                            Next
                        End If
                    Next j
                Next i
            End With

        interiorws.Delete

        Application.ScreenUpdating = True
        Application.DisplayAlerts = True

        End Sub

0 个答案:

没有答案