【问题标题】:Color cells based on cell value基于单元格值的颜色单元格
【发布时间】:2015-09-17 23:37:45
【问题描述】:

你能帮我看看我的代码有什么问题吗? 我收到一个错误:应用程序定义或对象定义错误。 我想根据单元格值为单元格着色。 如果单元格值等于单元格 1 值,则单元格应为红色。 此行出错: If Cells(i, 2).Value = Cells(i - 1, 3).Value Then

谢谢

Sub colorcells()
ThisWorkbook.Worksheets("Sheet1").Activate
Range("A1").Select
    Dim i As Long
    lastrow = Cells(Rows.Count, "B").End(xlUp).Row
    For i = 1 To lastrow
        If Cells(i, 2).Value = Cells(i - 1, 3).Value Then
            Cells(i, 1).Interior.ColorIndex = 3
        End If
    Next i
End Sub 

【问题讨论】:

  • i = 1 时,Cells(i - 1, 3) 会出现问题,因为没有第 0 行。

标签: vba excel


【解决方案1】:

如果您使用 Excel 2007 及更高版本,则可以使用条件格式而不是使用 VBA,这更容易使用。它为您提供了允许您设置规则的对话框。

【讨论】: