Talvez precise contar especificamente quantas ocorrências distintas existem num Range de dados.
Por exemplo: a, a, b, b, c, d, e, e, f = 5
Aqui está a solução fácil e rápida:
Public Function COUNTDISTINCTcol (ByRef rngToCheck As Range) As VariantDim colDistinct As CollectionDim varValues As Variant, varValue As VariantDim lngCount As Long, lngRow As Long, lngCol As LongOn Error GoTo ErrorHandlervarValues = rngToCheck.Value'if rngToCheck is more than 1 cell then'varValues will be a 2 dimensional arrayIf IsArray(varValues) ThenSet colDistinct = New CollectionFor lngRow = LBound(varValues, 1) To UBound(varValues, 1)For lngCol = LBound(varValues, 2) To UBound(varValues, 2)varValue = varValues(lngRow, lngCol)'ignore blank cells and throw error'if cell contains an error valueIf LenB(varValue) > 0 Then'if the item already exists then an error will'be thrown which we want to ignoreOn Error Resume NextcolDistinct.Add vbNullString, CStr(varValue)On Error GoTo ErrorHandlerEnd IfNext lngColNext lngRowlngCount = colDistinct.CountElseIf LenB(varValues) > 0 ThenlngCount = 1End IfEnd IfCOUNTDISTINCTcol = lngCountExit FunctionErrorHandler:COUNTDISTINCTcol = CVErr(xlErrValue)End Function
Nenhum comentário:
Postar um comentário