vba 2013条件格式和ColorIndex

时间:2016-08-11 22:31:10

标签: formatting conditional

不像thisthis, please

我想获得ColorIndex并求它; 4个细胞,一个范围。然后根据总数我知道我的下一个CF实际单元格。

我尝试了Ozgrid的所有内容,MrExcel,Ablebit,StackOCF for CPEarson接近需求。我没有看到INDIRECT INDIRECT(ADDRESS(ROW(), 7)) = 0

的感觉

所以在这里我们的功能似乎有点korky但它运行两次然后返回#VALUE!让我等了好几天。 即 [TRUE 172总计43 43 43 43 ==:TRUE是我的答案]

Function bgcolor(Rng As Range) As Boolean
 Dim R As Variant
 Dim C As Variant
 Dim AC As Integer
 Dim Total As Double

 AC = ActiveCondition(Rng)

For Each C In Rng.Cells
    For Each R In Rng.Cells
    Cells(R, C).Select

        If Selection.FormatConditions(R).Interior.ColorIndex = 2 Then
            Total = Total + 2
        Else
           If Selection.FormatConditions(R).Interior.ColorIndex = 43 Then
            Total = Total + 43
            Else
            Total = Total + 3
           End If
        End If

    Next R
Next C

Select Case Total
Case Total = -12423
    bgcolor = False
Case Total = -12383
    bgcolor = True
Case Total = -8278
    bgcolor = False
Case Total = -8198
    bgcolor = True
Case Total = -4133
    bgcolor = False
Case Total = -4013
    bgcolor = True
Case Total = 12
    bgcolor = False
Case Total = 52
    bgcolor = False
Case Total = 92
    bgcolor = False
Case Total = 132
    bgcolor = True
Case Total = 172
    bgcolor = True


    End Select

End Function

现在,它旁边的功能:

 Function ActiveCondition(Rng As Range) As Integer
 Dim Ndx As Long
 Dim FC As FormatCondition
 Dim Temp As Variant
 Dim Temp2 As Variant

 If Rng.FormatConditions.Count = 0 Then
    ActiveCondition = 0
 Else
     For Ndx = 1 To Rng.FormatConditions.Count
     Set FC = Rng.FormatConditions(Ndx)
     Select Case FC.Type
         Case xlCellValue
         Select Case FC.Operator
             Case xlBetween
                Temp = GetStrippedValue(FC.Formula1)
                Temp2 = GetStrippedValue(FC.Formula2)
                If IsNumeric(Temp) Then
                   If CDbl(Rng.Value) >= CDbl(FC.Formula1) And _
                       CDbl(Rng.Value) <= CDbl(FC.Formula2) Then
                       ActiveCondition = Ndx
                       Exit Function
                   End If
               Else
                  If Rng.Value >= Temp And _
                     Rng.Value <= Temp2 Then
                     ActiveCondition = Ndx
                     Exit Function
                  End If
               End If

            Case xlGreater
                Temp = GetStrippedValue(FC.Formula1)
                If IsNumeric(Temp) Then
                   If CDbl(Rng.Value) > CDbl(FC.Formula1) Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                Else
                   If Rng.Value > Temp Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                End If

            Case xlEqual
                Temp = GetStrippedValue(FC.Formula1)
                If IsNumeric(Temp) Then
                   If CDbl(Rng.Value) = CDbl(Temp) Then
                       ActiveCondition = Ndx
                       Exit Function
                   End If
                'Else
                   If Temp = Rng.Value Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                End If


            Case xlGreaterEqual
                Temp = GetStrippedValue(FC.Formula1)
                If IsNumeric(Temp) Then
                   If CDbl(Rng.Value) >= CDbl(Temp) Then
                       ActiveCondition = Ndx
                       Exit Function
                   End If
                Else
                   If Rng.Value >= Temp Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                End If


            Case xlLess
                Temp = GetStrippedValue(FC.Formula1)
                If IsNumeric(Temp) Then
                    If CDbl(Rng.Value) < CDbl(Temp) Then
                       ActiveCondition = Ndx
                       Exit Function
                    End If
                Else
                    If Rng.Value < Temp Then
                       ActiveCondition = Ndx
                       Exit Function
                    End If
                End If

            Case xlLessEqual
                Temp = GetStrippedValue(FC.Formula1)
                If IsNumeric(Temp) Then
                   If CDbl(Rng.Value) <= CDbl(FC.Formula1) Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                Else
                   If Rng.Value <= Temp Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                End If


            Case xlNotEqual
                Temp = GetStrippedValue(FC.Formula1)
                If IsNumeric(Temp) Then
                   If CDbl(Rng.Value) <> CDbl(FC.Formula1) Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                Else
                   If Temp <> Rng.Value Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                End If

           Case xlNotBetween
                Temp = GetStrippedValue(FC.Formula1)
                Temp2 = GetStrippedValue(FC.Formula2)
                If IsNumeric(Temp) Then
                   If Not (CDbl(Rng.Value) <= CDbl(FC.Formula1)) And _
                      (CDbl(Rng.Value) >= CDbl(FC.Formula2)) Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                Else
                   If Not Rng.Value <= Temp And _
                      Rng.Value >= Temp2 Then
                      ActiveCondition = Ndx
                      Exit Function
                   End If
                End If

           Case Else
                Debug.Print "UNKNOWN OPERATOR"
       End Select


       Case xlExpression
        If Application.Evaluate(FC.Formula1) Then
           ActiveCondition = Ndx
           Exit Function
        End If

      Case Else
        Debug.Print "UNKNOWN TYPE"
      End Select

    Next Ndx

 End If

   ActiveCondition = 0
 End Function

Function GetStrippedValue(CF As String) As String
' http://www.cpearson.com/excel/CFColors.htm
Dim Temp As String
If InStr(1, CF, "=", vbTextCompare) Then
   Temp = Mid(CF, 3, Len(CF) - 3)
   If Left(Temp, 1) = "=" Then
       Temp = Mid(Temp, 2)
   End If
Else
   Temp = CF
End If
 GetStrippedValue = Temp
End Function

确实,需要知道Activated Condition的运行位置,但是很难看出#VALUE!在第二种情况下仍然表现出来。

可能的检索值为:3或43或-4142或4次或其组合。

有它。希望你能,我不能。

1 个答案:

答案 0 :(得分:0)

后几个小时

我从“条件格式”问题转向“单元格颜色”,然后添加了一个excel公式并使用宏打印单元格颜色。

        If j = 28 Or j = 33 Or j = 54 Then

        With Selection
                .Interior.Color = xlNone
                If Cells(i, j).Value = "No events" Then
                    .Interior.Color = xlNone
                    .Font.Color = xlNone
                Else
                    If Cells(i, j).Value = True Then
                        .Interior.Color = RGB(153, 204, 0)
                        .Font.Color = RGB(153, 204, 0)
                    Else
                        .Interior.Color = RGB(255, 0, 0)
                        .Font.Color = RGB(255, 0, 0)
                    End If
                End If

        End With

        End If

这个CODE帮助我继续使用我已经使用逻辑的ColorIndex。

Function bgcolor(Rng As Range) As Integer
    Application.Volatile
    bgcolor = Rng.Interior.ColorIndex
End Function