比较2个单元格,然后比较下面的2个单元格

时间:2018-06-22 19:32:54

标签: vba

我是VBA的新手,现在已经坚持了几天。

我想比较H2和H3。如果相等,则将单元格变为绿色;如果不相等,则将单元格变为红色。 完成此操作后,我将对H4和H5进行相同的操作,然后对H6和H7进行同样的操作……一直到数据的最后一行。

预先感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

  Sub ForLoopTest()
  Dim loop_ctr As Integer
  Dim Max As Integer
  Max = ActiveSheet.UsedRange.Rows.Count

  For loop_ctr = 1 To Max
    If loop_ctr Mod 2 = 0 Then
      row_below = loop_ctr + 1
      If Cells(loop_ctr, "H") = Cells(row_below, "H") then
        Cells(loop_ctr, "H").Interior.ColorIndex = 4
        Cells(row_below, "H").Interior.ColorIndex = 4
      Else
        Cells(loop_ctr, "H").Interior.ColorIndex = 3
        Cells(row_below, "H").Interior.ColorIndex = 3
      End If
    End If
  Next loop_ctr
End Sub

答案 1 :(得分:0)

我仍然觉得条件格式是他们可以使用的方式,以便它对工作表中的值更改具有反应性,但是,如果您将VBA作为解决方案,这里应该采取以下措施:

Sub greenOrRed()
    Dim lngRow As Long
    For lngRow = 2 To Sheet1.Range("H2").End(xlDown).Row Step 2
        If Sheet1.Range("H" & lngRow).Value = Sheet1.Range("H" & lngRow + 1).Value Then
            Sheet1.Range("H" & lngRow & ":H" & lngRow + 1).Interior.ColorIndex = 4
        Else 'didn't match
            Sheet1.Range("H" & lngRow & ":H" & lngRow + 1).Interior.ColorIndex = 3
        End If
    Next lngRow
End Sub

您还可以使用For Each循环在该列中移动,这使一些阅读代码变得很不错。您只需在要分析的行上对Mod 2进行测试,而不必像上面的STEP 2循环中那样使用非常方便的For

Sub greenOrRed()
    Dim rngCell As Range
    For Each rngCell In Sheet1.Range("H:H").Cells
        If rngCell.Value = "" And rngCell.Row > 1 Then Exit For
        If rngCell.Row Mod 2 = 0 Then
            If rngCell.Value = rngCell.Offset(1).Value Then
                rngCell.Resize(2).Interior.ColorIndex = 4
            Else
                rngCell.Resize(2).Interior.ColorIndex = 3
            End If
        End If
    Next rngCell
End Sub

如果您真的想压缩它,可以对interior.ColorIndex的设置应用一些布尔数学,但这仅适用于红色和绿色相距1个colorindex值的情况。而且,下一个采用您的代码的人也会讨厌您,不会认为您的聪明程度与您想象的一样。

Sub greenOrRed()
    Dim rngCell As Range
    For Each rngCell In Sheet1.Range("H:H").Cells
        If rngCell.Value = "" And rngCell.Row > 1 Then Exit For
        If rngCell.Row Mod 2 = 0 Then rngCell.Resize(2).Interior.ColorIndex = 3 + Abs(rngCell.Value = rngCell.Offset(1).Value)
    Next rngCell
End Sub

答案 2 :(得分:0)

其他方式

另一种循环方法:

Sub CompareCells()
    Dim i As Long
    With Range("H2", Cells(Rows.Count,"H").End(xlUp)) ' reference column H cells from row 2 down to last not empty one
        For i = 1 To .Count Step 2 ' loop through referenced range skipping every other row
            With .Cells(i, 1) ' reference current cell
                .Interior.Color = IIf(.Value2 = .Offset(1).Value2, vbGreen, vbRed) 'set current cell color with respect to below cell content
            End With
        Next
    End With
End Sub

无循环方法:

Sub CompareCells()
    With Range("H2", Cells(Rows.Count, "H").End(xlUp)) ' reference column H cells from row 2 down to last not empty one
        With .Offset(, 1) ' reference referenced range 1 column to the right offset range. this is a "helpre" column
            .FormulaR1C1 = "=IF(even(row())=row(),1,"""")" ' write 1's every two rows in referenced range
            With .SpecialCells(xlCellTypeFormulas, xlNumbers) ' reference referenced range "numbered" rows
                .Offset(, -1).Interior.Color = vbRed ' mark referenced range 1 column left offset in red
                .FormulaR1C1 = "=IF(RC[-1]=R[1]C[-1],1,"""")" ' signal referenced range cells with 1 if corresponding 1 column to the left offset cell content equals its below cell content
                .SpecialCells(xlCellTypeFormulas, xlNumbers).Offset(, -1).Interior.Color = vbGreen ' turn reference referenced range "numbered" cells color to green
            End With
            .ClearContents ' clear referenced "helper" column
        End With
    End With
End Sub