[英]Conditional Formatting blank cells -VBA
I need to highlight a cell in column B if it is less than value in column F. For example if cell B2 is 10 and F2 is 20, B2 should be red.如果 B 列中的单元格小于 F 列中的值,我需要突出显示该单元格。例如,如果单元格 B2 为 10,F2 为 20,则 B2 应为红色。 However in column B there are blank cells i do not want these highlighted.
但是在 B 列中有空白单元格,我不希望这些突出显示。 For example B6 is blank but F6 is 10. In my code B6 become red as well.
例如 B6 是空白但 F6 是 10。在我的代码中 B6 也变成红色。
Also how would i highlight a cell in the same row that is already highlighted.另外,我将如何突出显示已突出显示的同一行中的单元格。 For example, if B2 is highlighted, highlight F2.
例如,如果 B2 突出显示,则突出显示 F2。
My code is below:我的代码如下:
Sub threecf()
Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition
Set rg = Range("B2", Range("B2").End(xlDown))
'clear any existing conditional formatting
rg.FormatConditions.Delete
'define the rule for each conditional format
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlLess, "=f2")
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=isempty(f2)")
'define the format applied for each conditional format
With cond1
.Interior.Color = vbRed
.Font.Color = vbWhite
End With
With cond2
.Interior.Color = vbWhite
.Font.Color = vbWhite
End With
End Sub
As mentioned in my comment, use formulas.正如我在评论中提到的,使用公式。 No need to use VBA
无需使用 VBA
Easiest Way (Recommended Way)最简单的方式(推荐方式)
I recommend this way because it takes into account new rows that are being added.我推荐这种方式,因为它考虑了正在添加的新行。
=AND(B1<F1,B1<>"")
=AND(B1<F1,B1<>"")
Customized Way定制方式
=AND(B2<F2,B2<>"")
=AND(B2<F2,B2<>"")
VBA Way VBA方式
If you still want VBA then try this如果你仍然想要 VBA,那么试试这个
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change as applicable
Set ws = Sheet1
With ws
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
With .Range("B2:B" & lRow)
.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(B2<F2,B2<>"""")"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
End With
End Sub
Go to conditional formatting --> New Rule --> Use a formula to determine which cells to format --> Paste This: =IF(AND(B2<F2,B2<>"") = TRUE,1,0)
转到条件格式 --> 新规则 --> 使用公式确定要设置格式的单元格 --> 粘贴此:
=IF(AND(B2<F2,B2<>"") = TRUE,1,0)
For the F column: =IF(AND(F2>B2,F2<>"") = TRUE,1,0)
对于 F 列:
=IF(AND(F2>B2,F2<>"") = TRUE,1,0)
If you want a VBA solution, try it without conditional formatting:如果您需要 VBA 解决方案,请在没有条件格式的情况下尝试:
Sub StackOverflow()
Dim x As Long
With ThisWorkbook.Sheets("Stack")
For x = 1 To .Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
If .Cells(x, 2).Value <> "" And .Cells(x, 2).Value < .Cells(x, 6).Value Then
.Cells(x, 2).Interior.Color = vbRed
.Cells(x, 6).Interior.Color = vbRed
.Cells(x, 2).Font.Color = vbWhite
.Cells(x, 6).Font.Color = vbWhite
Else
.Cells(x, 2).Interior.Pattern = xlNone
.Cells(x, 6).Interior.Pattern = xlNone
.Cells(x, 2).Font.Color = vbBlack
.Cells(x, 6).Font.Color = vbBlack
End If
Next x
End With
End Sub
Adapt the code to your needs (change the macro name, spreadsheet address and colors if you want).根据您的需要调整代码(如果需要,可以更改宏名称、电子表格地址和颜色)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.