我试图对C列中的单元格进行颜色编码,一个用于表示"是"和#34; No"。以下代码有效。但我想让它为C4和C4下的单元着色,而不是C1,C2和C3。有小费吗?还有任何提示我如何根据另一个工作表的结果对单元格进行颜色编码?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
For Each cell In ActiveSheet.UsedRange.Columns("C").Cells
If Len(cell.Value) > 2 Then
cell.Interior.ColorIndex = 3
ElseIf Len(cell.Value) < 3 Then
cell.Interior.ColorIndex = 4
End If
Next
End Sub
答案 0 :(得分:1)
使用:
N = Cells(Rows.Count, "C").End(xlUp).Row
For Each cell In Range("C4:C" & N)
答案 1 :(得分:0)
为什么不添加额外的if子句?
IE:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
For Each cell In ActiveSheet.UsedRange.Columns("C").Cells
If cell.Row <> 1 And cell.Row <> 2 And cell.Row <> 3 Then
If Len(cell.Value) > 2 Then
cell.Interior.ColorIndex = 3
ElseIf Len(cell.Value) < 3 Then
cell.Interior.ColorIndex = 4
End If
End If
Next
End Sub