[英]Color cells based on cell value
Can you help me what is wrong with my code. 您能帮我代码的问题吗? I get an error: Application defined or object defined error.
我收到一个错误:应用程序定义或对象定义的错误。 I would like to color a cell based on a cell value.
我想根据一个单元格值给一个单元格上色。 If a cell value equal to a cell-1 value the cell should be red.
如果像元值等于像元1的值,则像元应为红色。 Error with this line: If Cells(i, 2).Value = Cells(i - 1, 3).Value Then
这行错误:如果Cells(i,2).Value = Cells(i-1,3).Value然后
thank you 谢谢
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
Instead of using VBA, you can use conditional formatting if you are using Excel 2007 upwards, which is a lot easier to use. 如果您向上使用Excel 2007,则可以使用条件格式,而不是使用VBA,这更容易使用。 It provides you with dialog boxes which allow you to set rules.
它为您提供了一个对话框,允许您设置规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.