Bài viết: Hàm để lấy chỉ số màu trong các ô đã Conditional Formatting.

Liên hệ QC

SA_DQ

/(hông là gì!
Thành viên danh dự
Tham gia
8/6/06
Bài viết
14,317
Được thích
22,356
Nghề nghiệp
Nuôi ba ba & trùn quế
Xin phép giới thiệu với các bạn hàm để rút trích chỉ số màu đã được định dạng có điều kiện trong Conditional Formatting (CF). Để vậy, chúng ta phải xét đến hàm người dùng ActiveCondition sau đây:

PHP:
Option Explicit
Function ActiveCondition(Rng As Range) As Integer
Dim Tmp0, Tmp2, GPE As Long
Dim FC As FormatCondition

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

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

            Case xlEqual
                Tmp0 = GetStrippedValue(FC.Formula1)
                If IsNumeric(Tmp0) Then
                    If CDbl(Rng.Value) = CDbl(FC.Formula1) Then
                        ActiveCondition = GPE:           Exit Function
                    End If
                Else
                    If Tmp0 = Rng.Value Then
                        ActiveCondition = GPE:            Exit Function
                End If:                    End If

            Case xlGreaterEqual
                Tmp0 = GetStrippedValue(FC.Formula1)
                If IsNumeric(Tmp0) Then
                    If CDbl(Rng.Value) >= CDbl(FC.Formula1) Then
                        ActiveCondition = GPE:           Exit Function
                    End If
                Else
                    If Rng.Value >= Tmp0 Then
                        ActiveCondition = GPE:            Exit Function
                End If:                    End If
              
            Case xlLess
                Tmp0 = GetStrippedValue(FC.Formula1)
                If IsNumeric(Tmp0) Then
                    If CDbl(Rng.Value) < CDbl(FC.Formula1) Then
                        ActiveCondition = GPE:           Exit Function
                    End If
                Else
                    If Rng.Value < Tmp0 Then
                        ActiveCondition = GPE:           Exit Function
                End If:                    End If

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


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

            Case xlNotBetween
                Tmp0 = GetStrippedValue(FC.Formula1)
                Tmp2 = GetStrippedValue(FC.Formula2)
                If IsNumeric(Tmp0) Then
                    If Not (CDbl(Rng.Value) <= CDbl(FC.Formula1)) And _
                          (CDbl(Rng.Value) >= CDbl(FC.Formula2)) Then
                          ActiveCondition = GPE:            Exit Function
                    End If
                Else
                    If Not Rng.Value <= Tmp0 And _
                        Rng.Value >= Tmp2 Then
                        ActiveCondition = GPE:            Exit Function
                End If:                    End If
            
            Case Else
                Debug.Print "UNKNOWN OPERATOR"
            End Select

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

        Case Else
Debug.Print "UNKNOWN TYPE"
        End Select
Next GPE
End If
ActiveCondition = 0

End Function

Hàm ActiveCondition được bổ trợ bởi hàm dưới đây:

Mã:
Function GetStrippedValue(CF As String) As String
    Dim Tmp As String
    If InStr(1, CF, "=", vbTextCompare) Then
       Tmp = Mid(CF, 3, Len(CF) - 3)
       If Left(Tmp, 1) = "=" Then
           Tmp = Mid(Tmp, 2)
       End If
    Else
       Tmp = CF
    End If
    GetStrippedValue = Tmp
End Function

Khi trong tay chúng ta đã có công cụ trên, thì việc rút trích chỉ số màu đã ấn định trong CF chỉ còn là việc nhỏ nhoi, như hàm dưới đây:

PHP:
Function ColorIndexOfCF(Rng As Range, Optional OfFont As Boolean = False) As Integer
Dim AC As Integer

If Rng.FormatConditions.Count = 0 Then ''
    If OfFont Then
        ColorIndexOfCF = Rng.Font.ColorIndex
    Else
        ColorIndexOfCF = Rng.Interior.ColorIndex
    End If
Else
    AC = ActiveCondition(Rng)
    If OfFont Then
        ColorIndexOfCF = Rng.FormatConditions(AC).Font.ColorIndex
    Else
        ColorIndexOfCF = Rng.FormatConditions(AC).Interior.ColorIndex
    End If
End If

End Function
Các bạn cùng tôi làm chuyến khảo sát thực địa trong file đính kèm

Chú í: Hàm ActiveCondition có thể đưa ra kết quả không chính xác, nếu gặp các trường hợp sau đây:

1. Bạn gọi hàm từ worksheet cell

2. Các ô gán hàm đã được format "Formula Is" thay vì "Cell Value Is".

3. Công thức dùng trong Coditional Formula chứa các địa chỉ liên kết
 

File đính kèm

  • GPE.COM.rar
    18 KB · Đọc: 2
Chỉnh sửa lần cuối bởi điều hành viên:
Web KT
Back
Top Bottom