【问题标题】:vba 2013 conditional formatting sum ColorIndexvba 2013 条件格式总和 ColorIndex
【发布时间】:2016-12-18 19:49:57
【问题描述】:

没有什么像thisthis, please

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

我尝试了来自Ozgrid、MrExcel、Ablebit、StackOCF for CPEarson 的所有内容都接近需求。我看不到 INDIRECT INDIRECT(ADDRESS(ROW(), 7)) = 0 的意义

所以在这里我们的函数看起来有点古怪,但它运行了两次然后返回#VALUE!这让我等了好几天。 IE。 [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

确实,需要知道激活条件在哪里运行,但丑陋的#VALUE!在第二种情况下仍然出现。

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

有。希望你能,我不能。

【问题讨论】:

    标签: formatting conditional


    【解决方案1】:

    几个小时后

    我从条件格式问题转向单元格颜色,然后添加了一个 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
    

    此代码帮助我仍然使用我已经处理过逻辑的 ColorIndex。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2017-03-05
      • 2017-09-25
      • 1970-01-01
      • 2022-01-07
      相关资源
      最近更新 更多