如果不匹配,则比较列和颜色单元格

时间:2015-09-30 12:47:42

标签: excel-vba vba excel

我有两列我正在与另外两列进行比较。第一列'B'正在使用Column'XFD'进行检查,如果值不匹配,则单元格groundcolour会变为红色。同时,我检查列'C'是否与列'XFC'匹配。

如果值不匹配,我想为列'C'着色,但如果值匹配,我想编码改变颜色。请告知编码。

Column B  Column C     Column 'XFC    Column 'XFD' 
Q1          Jan-15      Jan-15           Q1
Q2          Oct-15      Feb-15           Q1
Q3          Jul-15      Mar-15           Q1
Q4          Dec-15      Apr-15           Q2
                        May-15           Q2
                        Jun-15           Q2
                        Jul-15           Q3
                        Aug-15           Q3
                        Sep-15           Q3
                        Oct-15           Q4
                        Nov-15           Q4
                        Dec-15           Q4

编码

Application.ScreenUpdating = False
  Dim stNow As Date
  Dim lastA As Long
  lastA = Range("B" & Rows.Count).End(xlUp).Row

  Dim lastB As Long
  lastB = Range("XFD" & Rows.Count).End(xlUp).Row
  Dim match As Boolean
  Dim i As Long, j As Long
  Dim r1 As Range, r2 As Range
  For i = 2 To lastA
   Set r1 = Range("B" & i)
   match = False
   For j = 2 To lastB
      Set r2 = Range("XFD" & j)
      If r1 = r2 Then
         match = True
         If Range("B" & i).Offset(0, 1) = Range("XFD" & j).Offset(0, -1) Then

            Range("B" & i).Offset(0, 1).Interior.Color = RGB(255, 0, 0)

         End If
      End If
   Next j
Next i

Application.ScreenUpdating = True

1 个答案:

答案 0 :(得分:0)

试试这个......

Application.ScreenUpdating = False
Dim stNow As Date
Dim lastA As Long
Dim lastB As Long
Dim i As Long
Dim j As Long
Dim r1 As Range
Dim r2 As Range
Dim i_store As Integer

lastA = Range("B" & Rows.Count).End(xlUp).Row
lastB = Range("XFD" & Rows.Count).End(xlUp).Row

For i = 2 To lastA
   Set r1 = Range("B" & i)
   i_store = 0
   For j = 2 To lastB
      Set r2 = Range("XFD" & j)
      If r1.Value = r2.Value Then
         If Range("B" & i).Offset(0, 1) = Range("XFD" & j).Offset(0, -1)   Then
            i_store = 0
            Exit For
         Else
            If i_store = 0 Then
               i_store = i
            End If
         End If
       End If
   Next j
   If i_store > 0 Then
      Range("B" & i_store).Offset(0, 1).Interior.Color = RGB(255, 0, 0)
   End If
 Next i
 Application.ScreenUpdating = True