2个不同的范围比较不同的工作表不工作VBA

时间:2015-07-22 16:32:53

标签: vba text colors compare

为什么不是这个文本比较工作?我试图比较不同纸张的2个不同范围。

有一个很好的简单方法吗?

Sub selecttest()

Text3 = Sheets("Input DATA").Range(Cells(2, 2), Cells(2, Columns.Count).End(xlToLeft))
Text4 = Sheets("SAP Output DATA").Range(Cells(3, 1), Cells(Rows.Count, 1).End(xlUp))

If StrComp(Text3, Text4, vbTextCompare) = 0 Then

Else

Cells(Cell.Row, "A").Interior.ColorIndex = 26
Cells(Cell.Row, "B").Interior.ColorIndex = 26
Cells(Cell.Row, "C").Interior.ColorIndex = 26
Cells(Cell.Row, "D").Interior.ColorIndex = 26

End If
End Sub

有什么东西我做错了吗?

也试过没有错误,但它不会解决:

Sub comprangetest()

With ThisWorkbook.Sheets("Input DATA")
Text3 = Range(Cells(2, 2), Cells(2, Columns.Count).End(xlToLeft)).Select
End With

With ThisWorkbook.Sheets("SAP Output DATA")
Text4 = Sheets("SAP Output DATA").Range(Cells(3, 1), Cells(Rows.Count, 1).End(xlUp)).Select
End With

'Text3 = Sheets("Input DATA").Range(Cells(2, 2), Cells(2, Columns.Count).End(xlToLeft))
'Text4 = Sheets("SAP Output DATA").Range(Cells(3, 1), Cells(Rows.Count, 1).End(xlUp))

If StrComp(Text3, Text4, vbTextCompare) = 0 Then

Else

ActiveSheet.Cells(Cell.Row, "A").Interior.ColorIndex = 26
ActiveSheet.Cells(Cell.Row, "B").Interior.ColorIndex = 26
ActiveSheet.Cells(Cell.Row, "C").Interior.ColorIndex = 26
ActiveSheet.Cells(Cell.Row, "D").Interior.ColorIndex = 26

End If

End Sub

我使用的方法是否正确?

1 个答案:

答案 0 :(得分:1)

不是从不同的纸张进行比较,而是将范围扩展到当前纸张,并且要求在比较开始之前使用选择。由于源范围在一行上,我使用k作为源所在的整数。源始终更改并始终高于选择。这条线用于比较。粗略的我现在甚至可以更进一步,从另一张纸创建一个选择范围。但这对我来说很有用。我希望我节省了一些时间让其他人像我一样苦苦挣扎。

Sub CompareRanges()
application.ScreenUpdating = False
    Dim Report As Worksheet
    Dim i As Integer, j As Integer, k As Integer
    Dim lastrow As Integer
    Dim LastColumn As Integer
    Dim sht As Worksheet
    Dim cell As Range
    Dim x As Long, y As Long

    Set sht = ThisWorkbook.Sheets("SAP Output DATA")

    lastrow = sht.UsedRange.Rows.Count
    LastColumn = sht.UsedRange.Columns.Count

    'If Selection Is Nothing Then
    'MsgBox "nothing selected, please select range."
    'Else

    'x is the first row number of selection, y is the last.
    x = Selection.Rows(1).row
    y = Selection.Rows.Count + x - 1
    'MsgBox x & "  " & y

    'give row number of cell above selection.
    k = Selection.Rows(1).Offset(-1, 0).row
    'MsgBox k

    For i = x To y 'lastrow
    'For i = 3 To lastrow 'lastrow
        For j = 5 To LastColumn
            If sht.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
                'sht.cell (2, j) "k is the variable for where is the source."
                If InStr(1, sht.Cells(k, j).Value, sht.Cells(i, 1).Value, vbTextCompare) > 0 Then
                    sht.Cells(i, 1).Interior.Color = RGB(255, 255, 255) 'White background
                    sht.Cells(i, 1).Font.Color = RGB(0, 0, 0) 'Black font color
                    Exit For
                Else
                    sht.Cells(i, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
                    sht.Cells(i, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
                End If
            End If
        Next j
    Next i

    'End If
application.ScreenUpdating = True
End Sub