Đếm chữ tô màu, nhưng không đếm trùng

Liên hệ QC

AnhThu-1976

Thành viên tích cực
Tham gia
17/10/14
Bài viết
1,054
Được thích
169
Em chào các anh chị
Em có sưu tầm được công thức đếm màu, nhưng nó đếm luôn những ký tự trùng nhau
Mã:
Function CountCellsByFontColor(rData As Range, cellRefColor As Range) As Long
    Dim indRefColor As Long
    Dim cellCurrent As Range
    Dim cntRes As Long

    Application.Volatile
    cntRes = 0
    indRefColor = cellRefColor.Cells(1, 1).Font.Color
    For Each cellCurrent In rData
        If indRefColor = cellCurrent.Font.Color Then
            cntRes = cntRes + 1
        End If
    Next cellCurrent

    CountCellsByFontColor = cntRes
End Function
Em muốn nó đếm màu nhưng loại trùng
Em muốn đếm ở cột A của sheet TH, kết quả mong muốn ở ô I1 là 3
Mong anh chị giúp đỡ. Em cảm ơn!
 

File đính kèm

  • DemKhongTrung.xlsm
    888.4 KB · Đọc: 3
Em chào các anh chị
Em có sưu tầm được công thức đếm màu, nhưng nó đếm luôn những ký tự trùng nhau
Mã:
Function CountCellsByFontColor(rData As Range, cellRefColor As Range) As Long
    Dim indRefColor As Long
    Dim cellCurrent As Range
    Dim cntRes As Long

    Application.Volatile
    cntRes = 0
    indRefColor = cellRefColor.Cells(1, 1).Font.Color
    For Each cellCurrent In rData
        If indRefColor = cellCurrent.Font.Color Then
            cntRes = cntRes + 1
        End If
    Next cellCurrent

    CountCellsByFontColor = cntRes
End Function
Em muốn nó đếm màu nhưng loại trùng
Em muốn đếm ở cột A của sheet TH, kết quả mong muốn ở ô I1 là 3
Mong anh chị giúp đỡ. Em cảm ơn!
Test code dưới xem sao
Mã:
Function CountCellsByFontColor(rData As Range, cellRefColor As Range) As Long
    Dim indRefColor As Long
    Dim cellCurrent As Range
    Dim cntRes As Long
    
    Dim sDic As String '<--

    Application.Volatile
    cntRes = 0
    indRefColor = cellRefColor.Cells(1, 1).Font.Color
    For Each cellCurrent In rData
        If indRefColor = cellCurrent.Font.Color Then
            If InStr(sDic, "#" & cellCurrent.Value & "#") = 0 Then
                sDic = "#" & cellCurrent.Value & "#" & sDic
                cntRes = cntRes + 1
            End If
        End If
    Next cellCurrent

    CountCellsByFontColor = cntRes
End Function
 
Web KT

Bài viết mới nhất

Back
Top Bottom