데이터 분석, 차트 작성, 전달을 위한 도구를 갖춘 Microsoft 스프레드시트 소프트웨어 제품군입니다.
이 응답은 자동 번역되었습니다. 따라서 문법 오류나 이상한 표현이 있을 수 있습니다.
안녕하세요. Q K2 고객
Microsoft 커뮤니티에 오신 것을 환영합니다.
특정 값인 경우에만 셀을 특정 색상으로 변경하려는 경우 문제를 이해합니다.
조건부 서식을 사용하여 완료할 셀 규칙을 강조 표시할 수 있습니다.
예를 들어 "a b c d"가 포함된 데이터 열을 만들고, 영역을 선택하고, 조건부 서식을 선택한 >, 셀 규칙에 > 같음 강조 표시를 선택합니다.
셀 "a"의 값은 노란색으로, "b"는 녹색으로, "c"는 연한 빨간색으로 설정됩니다.
설정이 완료되면 "b"에서 "c"까지의 값이 셀을 밝은 빨간색으로 바꿉니다.
그러나 이 방법을 사용하려면 더 많은 양의 데이터를 처리할 때 더 많은 규칙을 설정해야 하므로 재량에 따라 사용하는 것이 좋습니다.
이 작업을 수행하려면 VBA를 사용하여 셀 배경색에 따라 조건부 서식을 변경하는 함수를 만들 수 있습니다. 다음 VBA 코드에는 두 부분이 포함되어 있습니다.
- 셀 배경색을 기준으로 조건부 서식을 적용하는 기능입니다.
- 기존 조건부 서식을 "셀 텍스트 기준"에서 "셀 배경색 기준"으로 변환하는 함수입니다.
1부: 셀 배경색을 기반으로 조건부 서식 적용
이 기능은 조건부 서식을 적용하여 배경색이 노란색이고 셀 값이 "D"일 때 셀 색상을 빨간색으로 변경합니다.
Sub ApplyConditionalFormattingBasedOnBackgroundColor()
Dim ws As Worksheet
Dim cell As Range
Dim formatRange As Range
Dim colorCondition As FormatCondition
' Define the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
' Define the range you want to apply the formatting to
Set formatRange = ws.Range("A1:Z100") ' Adjust the range as needed
' Clear any existing conditional formatting in the range
formatRange.FormatConditions.Delete
' Loop through each cell in the range to apply the conditional formatting
For Each cell In formatRange
If cell.Interior.Color = RGB(255, 255, 0) Then ' Check if background color is yellow
' Add a new conditional format
Set colorCondition = cell.FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""D""")
colorCondition.Interior.Color = RGB(255, 0, 0) ' Set the fill color to red
End If
Next cell
MsgBox "Conditional formatting applied based on background color."
End Sub
파트 2: 조건부 서식을 텍스트 기반에서 배경색 기반으로 변환
이 함수는 셀 텍스트를 기반으로 하는 기존 조건부 서식 규칙을 셀 배경색을 기반으로 수정합니다.
Sub ConvertTextBasedToBackgroundColorBasedConditionalFormatting()
Dim ws As Worksheet
Dim cell As Range
Dim formatRange As Range
Dim colorCondition As FormatCondition
' Define the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
' Define the range you want to check for conditional formatting
Set formatRange = ws.Range("A1:Z100") ' Adjust the range as needed
' Loop through each cell in the range
For Each cell In formatRange
' Check if there are any conditional formatting rules
If cell.FormatConditions.Count > 0 Then
' Loop through each conditional formatting rule
For Each colorCondition In cell.FormatConditions
' Check if the condition is based on cell value
If colorCondition.Type = xlCellValue Then
' Check if the condition is based on text "D"
If colorCondition.Formula1 = "=""D""" Then
' Delete the existing condition
colorCondition.Delete
' Apply the new condition based on background color
If cell.Interior.Color = RGB(255, 255, 0) Then ' Check if background color is yellow
Set colorCondition = cell.FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""D""")
colorCondition.Interior.Color = RGB(255, 0, 0) ' Set the fill color to red
End If
End If
End If
Next colorCondition
End If
Next cell
MsgBox "Converted text-based conditional formatting to background color-based."
End Sub
VBA 코드 사용 지침
- Excel 통합 문서를 엽니다.
- Alt + F11 을 눌러 VBA 편집기를 엽니다.
- 모듈 삽입을 클릭하여 새 모듈을 삽입>.
- 제공된 VBA 코드를 복사하여 모듈에 붙여넣습니다.
- VBA 편집기를 닫습니다.
- Alt + F8을 누르고 함수(예: ApplyConditionalFormattingBasedOnBackgroundColor) 를 선택한 다음 실행을 클릭하여 원하는 함수를 실행합니다.
이러한 함수는 예제에 지정된대로 셀 배경색을 기반으로 조건부 서식을 적용하고 변환합니다. 특정 사용 사례에 필요한 범위와 색상을 조정합니다.
솔직히
카를로스 - MSFT | Microsoft 커뮤니티 지원 전문가