基于2个条件的条件格式化范围(其他单元格的格式/值)

时间:2016-09-07 16:03:15

标签: excel vba excel-vba conditional

我是VBA的新手,英语不是我的母语,所以这里就是。

我想条件格式行/范围(给它们绿色背景)如果该行中的单元格C在列C中具有重复值,并且如果列O中的单元格等于0,但是如果列中的单元格C没有类似的值,不要将条件格式应用于该单元格(尽管O列中的单元格的值为0)。

注意:在C列中具有相同值的单元格将始终在彼此之上和之下,例如,C1 = C2 = C3但可能不是C1<> C2,C1 = C3

我知道我没有清楚解释,所以如果您想了解更多信息,请告诉我。

更新(更多信息):我可能有3行或更多行具有相同的C列值,彼此之上和之下,并且列O中的零值将始终是底行。

实施例: 如果C1 = C2 = C3 = C4 = C5且O5 = 0,则行1 2 3 4 5变为绿色。 我更喜欢使用条件格式,即使它需要vba代码,所以我不必每次在O列中都有新的0时运行它。

我已经使用过这段代码,但它显然不起作用,但可能与我的问题有点不同,因为真实的数据比我说明的要复杂得多。我的数据表从第4行开始(第3行标题)。此代码仅格式化1行(在列O值为零的行上方),我需要的是格式化具有相同列C值的所有行。请记住,我是vba的新手:(

With Range("A4:r8000").FormatConditions.Add( _ 
Type:=xlExpression, _
 Formula1:="=AND($C4=$C5,$O5=0,$F4<>0)")
 .Interior.Color = 13551615
 .Font.Color = -16383844
End With

3 个答案:

答案 0 :(得分:0)

尝试将此作为CFR的公式,

PreCachingLayoutManager layoutManager = new  
PreCachingLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setExtraLayoutSpace(DeviceUtils.getScreenHeight(getActivity()));
recyclerView.setLayoutManager(layoutManager);

答案 1 :(得分:-1)

如果具有相同值的单元格始终分组(一个在另一个之下),则以下代码可能会执行您想要的操作。

=and(countif(c:c, c1)>1, o1=0, len(o1))
'alternate for part that I am not sure I understand
=and(countif(c$1:c1, c1)>1, o1=0, len(o1))

答案 2 :(得分:-1)

如果有任何重复的单元格,这将通过并突出显示重复的单元格。行有&#39; 0&#39;在O列中。我仍在开发一种方法,只要在列O中发生更改,就会自动更新,但无法完全解决这个问题。我会更新。

Sub ConditionalFormatSE()

Application.ScreenUpdating = False

Dim lastRow As Long
Dim myCell As Range
Dim colCVals As Range

lastRow = Cells(Rows.Count, 3).End(xlUp).Row

Set colCVals = Range("C1", "C" & lastRow)

colCVals.clearformats

For Each myCell In colCVals
    If Cells(myCell.Row, 15).Value = "0" Then
        If WorksheetFunction.CountIf(colCVals, myCell.Value) > 1 Then
            Set c = colCVals.Find(myCell.Value)
            If Not c Is Nothing Then
                firstAddress = c.Address
                Do
                    c.Interior.color = RGB(198, 239, 206)
                    c.Font.color = RGB(0, 97, 0)
                    Set c = colCVals.FindNext(c)
                Loop While Not c Is Nothing And c.Address <> firstAddress
            End If
        End If
    End If
Next myCell

Set colCVals = Nothing
Set myCell = Nothing
Set c = Nothing

Application.ScreenUpdating = True

End Sub

至于使其自动运行,请将其放入:VBAProject([workbookname] .xlsm) - &gt; Microsoft Excel Objects-&gt; Sheet1([sheetname]),它应该在列&#39;中的值时运行。 O&#39;已更改

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

Set KeyCells = Columns(15)

If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
       Call ConditionalFormatSE
End If

Set KeyCells = Nothing
End Sub