Excel电子表格中的条件格式

时间:2016-04-22 07:58:38

标签: excel vba excel-vba

Picture of some data

这是我的要求 1.只是每行的最低值将以蓝色改变其颜色(如果最低值在多个单元格中) 2.如果最低值仅为一种颜色,则单元格为黄色。

这些都是仅使用每行中具有最低值的单元格完成的。

2 个答案:

答案 0 :(得分:0)

我在下面创建的公式是为A到N列设置的,可根据需要进行调整。

使用以下规则条件格式化单元格A1(根据需要调整):


=IF(IF(A1=SMALL($A1:$N1,1),COUNTIF($A1:$N1,SMALL($A1:$N1,1)),0)>=2,1,0)

将此标记为“停止如果为真”'并将背景颜色调为黄色。


同一个小区的下一个规则:

=IF(A1=SMALL($A1:$N1,1),1,0)

将背景颜色为蓝色。


使用format painter将规则复制到该行中的其他单元格。

单元格现在应该根据需要更改背景颜色。

答案 1 :(得分:0)

试一试。它并不完美但应该做你想做的事。

Sub ConditionalFormat()

    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With

    Dim ws As Worksheet
    Dim x As Integer, u As Integer, myCount As Integer
    Dim lRow As Long, lColumn As Long
    Dim myMin As Double
    Set ws = Worksheets("Sheet1")

    With ws
        'Count last row
        lRow = .Cells.Find(What:="*", _
            After:=.Cells(1, 1), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Row
        'Count last column
        lColumn = .Cells.Find(What:="*", _
            After:=.Cells(1, 1), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Column
        'Remove currency numberformat
        .Range(.Cells(1, 1), .Cells(lRow, lColumn)).NumberFormat = "General"

        For x = 1 To lRow
            'Find smallest value and count how many times it shows up
            myMin = Application.WorksheetFunction.Min(.Rows(x))
            myCount = Application.WorksheetFunction.CountIf(.Rows(x), myMin)
            'Color cells that appear more than once blue
            If myCount > 1 Then
                For u = 1 To lColumn
                    If .Cells(x, u).Value = myMin Then
                        .Cells(x, u).Interior.Color = 15773696
                    End If
                Next u
            'Color cells that appear once yellow
            ElseIf myCount = 1 Then
                For u = 1 To lColumn
                    If .Cells(x, u).Value = myMin Then
                        .Cells(x, u).Interior.Color = 65535
                        Exit For
                    End If
                Next u
            End If
        Next x
    'Return currency format
    .Range(.Cells(1, 1), .Cells(lRow, lColumn)).NumberFormat = "[$£-809]#,##0.00"

    End With

    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With

End Sub