繁体   English   中英

Excel宏–根据值更改行

[英]Excel Macro – Change the row based on value

我想更改下面的宏,以使其根据单元格值更改行的一部分而不是单元格的颜色。

如果单元格E2中的值是“标准”,则单元格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

如果要处理的行很多,则可能希望放弃循环过程,而改为使用过滤器。

对于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

我没有通配搜索证据,但这只是一个小小的修改。 似乎您的原始代码正在寻找整个单元格值。

使用条件格式,选择ColumnsA:E,主页>条件格式,新规则..., 使用公式确定要格式化的单元格在该公式为true的情况下设置值的 格式 ::

=$E1="proof"

格式... ,选择红色,确定,确定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM