Я продолжаю получать сообщение об ошибке "Если Report.Cells (i, 18) .Value <>" "Тогда в строке. Я знаю, что, должно быть, здесь что-то глупо упущено ...
Просто пытаюсь сравнить столбцы 18 и 20 и выделить различия.
Я знаю, что это можно сделать и другими способами ...
Sub Compare_Columns()
'Get the last row
Dim Report As Worksheet
Dim i As Integer, j As Integer
Dim lastRow As Integer
'Set Report = Excel.Worksheets("Sheet1") 'You could also use Excel.ActiveSheet if you always want this to run on the current sheet.
Set Report = Excel.ActiveSheet
lastRow = Report.UsedRange.Rows.Count
Application.ScreenUpdating = False
For i = 2 To lastRow
For j = 2 To lastRow
If Report.Cells(i, 18).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(j, 20).Value, Report.Cells(i, 18).Value, vbTextCompare) > 0 Then
Report.Cells(i, 18).Interior.Color = RGB(255, 255, 255) 'White background
Report.Cells(i, 18).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(i, 18).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(i, 18).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next j
Next i
'Now I use the same code for the second column, and just switch the column numbers.
For i = 2 To lastRow
For j = 2 To lastRow
If Report.Cells(i, 20).Value <> "" Then
If InStr(1, Report.Cells(j, 18).Value, Report.Cells(i, 20).Value, vbTextCompare) > 0 Then
Report.Cells(i, 20).Interior.Color = RGB(255, 255, 255) 'White background
Report.Cells(i, 20).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(i, 20).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(i, 20).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next j
Next i
Application.ScreenUpdating = True
End Sub