Vba: 셀에 조건부 서식을 적용하는 방법


VBA에서 다음 방법을 사용하여 셀에 조건부 서식을 적용할 수 있습니다.

방법 1: 조건에 따라 조건부 서식 적용

 Sub ConditionalFormatOne()
Dim rg As Range
Dim cond As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" B2:B11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'apply conditional formatting to any cell in range B2:B11 with value greater than 30
Set cond = rg.FormatConditions.Add(xlCellValue, xlGreater, " =30 ")

'define conditional formatting to use
With cond
.Interior.Color = vbGreen
.Font.Color = vbBlack
.Font.Bold = True
End With

End Sub

방법 2: 여러 조건에 따라 조건부 서식 적용

 Sub ConditionalFormatMultiple()
Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" A2:A11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'specify rules for conditional formatting
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Mavericks ”)
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Blazers ”)
Set cond3 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Celtics ”)

'define conditional formatting to use
With cond1
.Interior.Color = vbBlue
.Font.Color = vbWhite
.Font.Italic = True
End With

With cond2
.Interior.Color = vbRed
.Font.Color = vbWhite
.Font.Bold = True
End With

With cond3
.Interior.Color = vbGreen
.Font.Color = vbBlack
End With

End Sub

방법 3: 셀에서 모든 조건부 서식 규칙 제거

 Sub RemoveConditionalFormatting()
ActiveSheet.Cells.FormatConditions.Delete
End Sub

다음 예에서는 Excel에서 다음 데이터 세트를 사용하여 실제로 각 방법을 사용하는 방법을 보여줍니다.

예 1: 조건에 따라 조건부 서식 적용

다음 매크로를 사용하여 B2:B11 범위에 있는 30보다 큰 값을 가진 셀을 녹색 배경, 검은색 글꼴 및 굵은 텍스트 스타일로 채울 수 있습니다.

 Sub ConditionalFormatOne()
Dim rg As Range
Dim cond As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" B2:B11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'apply conditional formatting to any cell in range B2:B11 with value greater than 30
Set cond = rg.FormatConditions.Add(xlCellValue, xlGreater, " =30 ")

'define conditional formatting to use
With cond
.Interior.Color = vbGreen
.Font.Color = vbBlack
.Font.Bold = True
End With

End Sub

이 매크로를 실행하면 다음과 같은 출력이 나타납니다.

B2:B11 범위에서 값이 30보다 큰 각 셀에는 조건부 서식이 적용됩니다.

30보다 작거나 같은 값을 가진 모든 셀은 그대로 유지됩니다.

예 2: 여러 조건을 기반으로 조건부 서식 적용

다음 매크로를 사용하여 팀 이름을 기준으로 A2:A11 범위의 셀에 조건부 서식을 적용할 수 있습니다.

 Sub ConditionalFormatMultiple()
Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" A2:A11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'specify rules for conditional formatting
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Mavericks ”)
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Blazers ”)
Set cond3 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Celtics ”)

'define conditional formatting to use
With cond1
.Interior.Color = vbBlue
.Font.Color = vbWhite
.Font.Italic = True
End With

With cond2
.Interior.Color = vbRed
.Font.Color = vbWhite
.Font.Bold = True
End With

With cond3
.Interior.Color = vbGreen
.Font.Color = vbBlack
End With

End Sub

이 매크로를 실행하면 다음과 같은 출력이 나타납니다.

팀 이름이 “Mavericks”, “Blazers” 및 “Celtics”인 셀에는 모두 특정 조건부 서식이 적용됩니다.

“Lakers”라는 이름을 가진 유일한 팀은 해당 팀 이름을 가진 셀에 대해 조건부 서식 규칙을 지정하지 않았기 때문에 홀로 남습니다.

예 3: 셀에서 모든 조건부 서식 규칙 제거

마지막으로 다음 매크로를 사용하여 현재 시트의 셀에서 모든 조건부 서식 규칙을 제거할 수 있습니다.

 Sub RemoveConditionalFormatting()
ActiveSheet.Cells.FormatConditions.Delete
End Sub

이 매크로를 실행하면 다음과 같은 출력이 나타납니다.

모든 조건부 서식이 각 셀에서 제거되었습니다.

추가 리소스

다음 자습서에서는 VBA에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.

VBA: 문자열에서 문자 발생 횟수를 계산하는 방법
VBA:문자열에 다른 문자열이 포함되어 있는지 확인하는 방법
VBA: 셀에 “If”가 포함되어 있는지에 대한 수식

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다