Excelmacros – 根据值更改行

我想改变下面的macros,使它改变行的一部分,而不是单元格的颜色,基于单元格的值。

如果单元格E2中的值是“certificate”,则单元格A2-E2变成红色。

Sub ChangeColor() lRow = Range("E" & Rows.Count).End(xlUp).Row Set MR = Range("E2:E" & lRow) For Each cell In MR If cell.Value = "Proof" Then cell.Interior.ColorIndex = 3 Next End Sub 

这是一个相对简单的改变。 将cell.Interior.ColorIndex = 3更改为特定范围,如下面的过程所示。

 Sub ChangeColor() lRow = Range("E" & Rows.Count).End(xlUp).Row Set MR = Range("E2:E" & lRow) For Each cell In MR If cell.Value = "Proof" Then range("a" & cell.row & ":e" & cell.row).Interior.ColorIndex = 3 Next End Sub 

如果你有很多行要处理,你可能希望放弃循环过程,而使用filter。

对于A:E突出显示:

 Sub highlight_Proof() With ActiveSheet With .Cells(1, 1).CurrentRegion .Cells.Interior.Pattern = xlNone If .AutoFilter Then .AutoFilter .AutoFilter Field:=5, Criteria1:="=proof" With .Offset(1, 0).Resize(.Rows.Count - 1, 5) If CBool(Application.Subtotal(103, .Cells)) Then _ .SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3 End With .AutoFilter End With End With End Sub 

对于全行突出显示:

 Sub highlight_Proof2() With ActiveSheet With .Cells(1, 1).CurrentRegion If .AutoFilter Then .AutoFilter .AutoFilter Field:=5, Criteria1:="=proof" With .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) If CBool(Application.Subtotal(103, .Cells)) Then _ .SpecialCells(xlCellTypeVisible).EntireRow.Interior.ColorIndex = 3 End With .AutoFilter End With End With End Sub 

我还没有通过searchcertificate,但这是一个小的修改。 看来你的原始代码正在寻找整个单元格的值。

使用条件格式化,selectColumnsA:E,HOME> Conditional Formatting,New Rule …, 使用公式确定要格式化的单元格

 =$E1="proof" 

格式化... ,select红色,确定,确定。