如何在进行更改时以黄色突出显示 Excel 中的一行并将单元格突出显示为红色(更改不止一个)

How to highlight a line in Exel yellow and the cells red when making changes (with more than one change)

我正在使用下面的公式在对单元格进行更改时以黄色突出显示一行,将单元格突出显示为红色。问题是有时我们想对一行进行多次更改,并希望所有发生更改的单元格都保持红色,但是当更改行中的第二个单元格时,此公式会清除第一个红色单元格。我可以添加一些例外以防止在进行第二次更改时已经是红色的单元格变为黄色吗?

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
    Target.EntireRow.Interior.Color = vbYellow
    Target.Interior.Color = vbRed
End Sub

请尝试此代码。

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    Dim Cl      As Long                 ' last used column

    With Target
        If .CountLarge = 1 Then
            ' change .Row to longest used row number
            ' if your rows aren't of uniform length
            If Sh.Cells(.Row, "A").Interior.Color <> vbYellow And _
               Sh.Cells(.Row, "A").Interior.Color <> vbRed Then
                Cl = Sh.Cells(.Row, Columns.Count).End(xlToLeft).Column
                Sh.Range(Sh.Cells(.Row, 1), Sh.Cells(.Row, Cl)).Interior.Color = vbYellow
            End If
            .Interior.Color = vbRed
        End If
    End With
End Sub

以上程序未经测试。事实上,我想知道你为什么使用 Workbook 事件。但是,我确实测试了下面的功能,也许这就是您所需要的。

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Cl      As Long                 ' last used column

    With Target
        If .CountLarge = 1 Then
            ' change .Row to longest used row number
            ' if your rows aren't of uniform length
            If Cells(.Row, "A").Interior.Color <> vbYellow And _
               Cells(.Row, "A").Interior.Color <> vbRed Then
                Cl = Cells(.Row, Columns.Count).End(xlToLeft).Column
                Range(Cells(.Row, 1), Cells(.Row, Cl)).Interior.Color = vbYellow
            End If
            .Interior.Color = vbRed
        End If
    End With
End Sub