IT/Digital Life
엑셀에서 중복값끼리 같은 색 으로 설정하기 _ 그룹별로 다른 색 설정하기 (VBA)
르미르미
2024. 10. 17. 19:20
1. VBA 편집기 열기
ALT+F11
INSERT > Module
2. VBA 코드 입력 (범위 설정)
Sub ColorDuplicates()
Dim rng As Range
Dim cell As Range
Dim colorIndex As Integer
Dim dict As Object
Dim countDict As Object
Set dict = CreateObject("Scripting.Dictionary")
Set countDict = CreateObject("Scripting.Dictionary")
' 데이터 범위를 정의 (여기서는 A1:A100 범위를 사용)
Set rng = Range("A1:A100")
' 색상을 순서대로 사용할 인덱스 (56개 색상 사용 가능)
colorIndex = 3 ' 3은 녹색 (다른 값은 다른 색을 의미함)
' 각 값의 출현 횟수를 계산
For Each cell In rng
If Not IsEmpty(cell.Value) Then
If countDict.exists(cell.Value) Then
countDict(cell.Value) = countDict(cell.Value) + 1
Else
countDict.Add cell.Value, 1
End If
End If
Next cell
' 각 셀을 검사하여 중복 그룹별로 색상 지정
For Each cell In rng
If Not IsEmpty(cell.Value) Then
' 중복된 값일 경우에만 색상 지정
If countDict(cell.Value) > 1 Then
If dict.exists(cell.Value) Then
cell.Interior.ColorIndex = dict(cell.Value)
Else
' 새로운 값일 경우 색상을 지정하고 딕셔너리에 추가
dict.Add cell.Value, colorIndex
cell.Interior.ColorIndex = colorIndex
' 색상 인덱스 순환 (56가지 색상 이후 초기화)
colorIndex = colorIndex + 1
If colorIndex > 56 Then colorIndex = 3
End If
Else
' 단일값은 색상을 제거
cell.Interior.ColorIndex = xlNone
End If
End If
Next cell
End Sub
3. 코드 실행
F5