在单元格foobar中突出显示所有等于值的行

我试图编写这个过程,以突出显示其N列中各自行中的值为“N”的所有行

我对编码VBA格式不太熟悉,我不能让这个程序运行

Sub highlight_new_pos() Dim rng As Range, lCount As Long, LastRow As Long Dim cell As Object With ActiveSheet 'set this worksheet properly! LastRow = .Cells(Rows.Count, 1).End(xlUp).Row For Each cell In .Range("N2:N" & LastRow) If cell = "N" Then With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With Next cell End With End Sub 

 Option Explicit Sub highlight_new_pos() Dim cel As Object With ActiveSheet For Each cel In .Range("N2:N" & .Cells(.Rows.Count, 14).End(xlUp).Row) If UCase(cel.Value2) = "N" Then cel.Interior.Color = 65535 Next End With End Sub 

如果你有很多行,这将会更快:

 Sub highlight_new_pos1() Application.ScreenUpdating = False With ActiveSheet With .Range("N1:N" & .Cells(.Rows.Count, 14).End(xlUp).Row) .AutoFilter Field:=1, Criteria1:="N" .Offset(1, 0).Resize(.Rows.Count - 14, .Columns.Count).Interior.Color = 65535 .AutoFilter End With End With Application.ScreenUpdating = True End Sub 

在你的代码中,你在循环单元格,但是你仍然在改变初始select的颜色(而不是循环中的单元格)。 调整如下:

 Sub highlight_new_pos() Dim rng As Range, lCount As Long, LastRow As Long Dim cell As Object With ActiveSheet 'set this worksheet properly! LastRow = .Cells(Rows.Count, 1).End(xlUp).Row For Each cell In .Range("N2:N" & LastRow) If cell = "N" Then With cell.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With End if Next cell End With End Sub 

如果你想要整行,改变cell.Interiorcell.entirerow.Interior