通过列看,突出显示行。

我有一个表“数据”,我有从G到P填充价格列。 我在同一张纸上有另一栏U,它有最高的价格。 (目标价)。

我现在要检查每一行,如果G到P之间的任何一列的价格超过了我列U中的目标价格,那么我想用红色突出显示整行。

我尝试了下面的代码,但它的function。 任何人都可以build议,我怎样才能看到每一列和比较目标和突出显示。 如果你可以评论你的代码,这将是非常好的。

Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim i As Integer For i = 7 To 16 If Cells(Target.Row, 21).Value <= Cells(Target.Row, i).Value Then Target.EntireRow.Interior.Color = vbRed End If Next i For i = 7 To 16 If Cells(Target.Row, 21).Value > Cells(Target.Row, i).Value Then Target.EntireRow.Interior.ColorIndex = xlNone End If Next i If Cells(Target.Row, 22).Value = "x" Then Target.EntireRow.Interior.ColorIndex = xlNone End If End Sub 

我仍然可以在条件格式的帮助下做到这一点,尽pipe通过VBA应用,如你所坚持的。

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.CountLarge > 1 Then Exit Sub Dim lr As Long Dim rng As Range If Target.Row >= 7 And Not Intersect(Target, Range("G:P")) Is Nothing Then lr = Cells(Rows.Count, "U").End(xlUp).Row Set rng = Range("G7:U" & lr) With rng .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:= _ "=AND(ROW($G7)>=7,G7<>"""",$U7<>"""",MAX($G7:$P7)>=$U7)" .FormatConditions(rng.FormatConditions.Count).SetFirstPriority .FormatConditions(1).Interior.Color = 65535 .FormatConditions(1).StopIfTrue = False End With End If End Sub 

尝试这个

 Private Sub Worksheet_Change(ByVal Target As Range) If Cells(Target.Row, 22).Value = "x" Or Application.Max(Target.Rows) < Cells(Target.Row, 21) Then Cells(Target.Row, 1).EntireRow.Interior.Color = xlNone Else Cells(Target.Row, 1).EntireRow.Interior.Color = vbRed End If End Sub