본문 바로가기

IT/엑셀(Excel)

[Excel-VBA] Cell 색상 설정 - ColorIndex, RGB

반응형

VBA 를 이용해서 색상을 설정하는 방법은 아래와 같이 ColorIndex 를 이용하거나, RGB값을 이용하는 방법이 있다

Cells(1,1).Interior.ColorIndex = 1   'ColorIndex 이용
Cells(1,1).Interior.Color =RGB(255,255,255)  'RGB값 이용

Color Index 색상을 hex 값과 RGB 값으로 확인하는 소스는 아래와 같다

소스와 결과 Sheet

Sub ColorIndex()
    
    Dim i As Integer
    Dim aCell As Range
    Dim tmp As String
    
    For i = 1 To 56
        Cells(i, 1) = "ColorIndex : " & i
        Cells(i, 1).ColumnWidth = 18
        
        Cells(i, 2).Interior.ColorIndex = i  ' Cell 색상 변경
        
        ' Hex 값
        tmp = Right("000000" & Hex(Cells(i, 2).Interior.Color), 6)
        Cells(i, 3) = "#" & Right(tmp, 2) & Mid(tmp, 3, 2) & Left(tmp, 2)
        Cells(i, 3).ColumnWidth = 10
        
        'RGB값
        Cells(i, 4).Formula = "=Hex2dec(""" & Right(tmp, 2) & """)"
        Cells(i, 5).Formula = "=Hex2dec(""" & Mid(tmp, 3, 2) & """)"
        Cells(i, 6).Formula = "=Hex2dec(""" & Left(tmp, 2) & """)"
    Next i
    
    
End Sub

 

반응형