Как сравнить символы между двумя столбцами и выделить конкретные ячейки, которые являются одинаковыми, с помощью макроса - PullRequest
0 голосов
/ 14 февраля 2019

Мне нужна ваша помощь, чтобы сравнить два столбца каждого символа ячейки.Для примера в ячейке данных A2 у меня есть RA_R3D_CS, здесь мне нужно выбрать R3D и сравнить его со значениями столбца B2 (R3D-172) первых трех символов, и если оба они одинаковы, мне нужно выделить его зеленым цветом, если не хочувыделить его на красный.

Точно так же я должен сравнить оставшееся и это, поэтому, пожалуйста, дайте мне реальное решение для решения этой проблемы.

Заранее спасибо.

Я пробовал нижекод, но я не получаю точный вывод.

Sub compare_cols()

    '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.

    lastRow = Report.UsedRange.Rows.Count

    Application.ScreenUpdating = False

    For i = 2 To lastRow
        For j = 2 To lastRow
            If Report.Cells(i, 1).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, 2).Value, Report.Cells(i, 1).Value, vbTextCompare) > 0 Then
                    'You may notice in the above instr statement, I have used vbTextCompare instead of its numerical value, _
                    I find this much more reliable.
                    Report.Cells(i, 1).Interior.Color = RGB(255, 255, 255) 'White background
                    Report.Cells(i, 1).Font.Color = RGB(0, 0, 0) 'Black font color
                    Exit For
                Else
                    Report.Cells(i, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
                    Report.Cells(i, 1).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, 2).Value <> "" Then
                If InStr(1, Report.Cells(j, 1).Value, Report.Cells(i, 2).Value, vbTextCompare) > 0 Then
                    Report.Cells(i, 2).Interior.Color = RGB(255, 255, 255) 'White background
                    Report.Cells(i, 2).Font.Color = RGB(0, 0, 0) 'Black font color
                    Exit For
                Else
                    Report.Cells(i, 2).Interior.Color = RGB(156, 0, 6) 'Dark red background
                    Report.Cells(i, 2).Font.Color = RGB(255, 199, 206) 'Light red font color
                End If
            End If
        Next j
    Next i

Application.ScreenUpdating = True

End Sub

1 Ответ

0 голосов
/ 14 февраля 2019

Решение без VBA

Представьте себе следующую таблицу (столбец B также может быть в другом листе)

enter image description here

Добавить условное форматирование для диапазона A1: B3, используя следующую формулу…

=left($A1,3)=left($B1,3)

… и получите следующий результат, который помечает элементы с теми же 3 символами в начале зеленого цвета.

enter image description here

Подробнее о Условное форматирование .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...