Dim i As Byte, k As Byte, clrVal As Long, clrValOccupied As Boolean, myRng As Range, cell As Range, j As Byte
Set myRng = Selection
'urcenie nutneho poctu farieb na zaklade poctu duplicit
j = Evaluate("SUM(1/COUNTIF(" & myRng.Address & ",""""&" & myRng.Address & ")) - SUM(--(COUNTIF(" & myRng.Address & ", " & myRng.Address & ")=1))")
ReDim clrArr(1 To j) As Long
'vytvorenie unikatnej kolekcie farieb
For k = 1 To j
clrValOccupied = False
clrVal = RGB(WorksheetFunction.RandBetween(0, 255), WorksheetFunction.RandBetween(0, 255), WorksheetFunction.RandBetween(0, 255))
For i = 1 To k
If clrArr(i) = clrVal Then
clrValOccupied = True
Exit For
End If
Next i
If clrValOccupied = False Then
clrArr(k) = clrVal
Else: k = k - 1
End If
Next k
myRng.Interior.Pattern = xlNone 'vymaze farby
k = 0
For Each cell In myRng.Cells
If WorksheetFunction.CountIf(myRng, cell) > 1 Then ' existuju duplicity
If WorksheetFunction.Match(cell, myRng, 0) = cell.Row - myRng.Cells(1, 1).Row + 1 Then 'prvy vyskyt duplicity
k = k + 1
cell.Interior.Color = clrArr(k)
Else: cell.Interior.Color = myRng.Cells(WorksheetFunction.Match(cell, myRng, 0), 1).Interior.Color
End If
End If
Next cell
End Subcitovat