엑설 조건부 서식 셀 문자 기준 말고 셀 배경 색상 기준으로 바뀌는거 기능도 만들어 주세요

익명
2024-05-21T23:32:09+00:00

엑설 조건부 서식 중 "셀 문자 기준" 말고 "셀 배경 색상 기준"으로 바뀌는거 기능도 만들어 주세요

예를들어

셀 배경 색상이 노랑색일때 D 입력시 빨간색으로 변하게

vba로 할수는 있는데 코드 짜기 너무 힘이듭니다

Microsoft 365 및 Office | Excel | 가정용 | Windows

잠긴 질문. 이 질문은 Microsoft 지원 커뮤니티에서 마이그레이션되었습니다. 질문이 도움이 되었는지 여부에 대해 응답할 수는 있지만, 메모나 회신을 추가하거나 질문을 따를 수는 없습니다.

댓글 0개 설명 없음

답변 1개

정렬 기준: 가장 유용함
  1. 익명
    2024-05-22T09:02:11+00:00

    이 응답은 자동 번역되었습니다. 따라서 문법 오류나 이상한 표현이 있을 수 있습니다.

    안녕하세요. Q K2 고객

    Microsoft 커뮤니티에 오신 것을 환영합니다.

    특정 값인 경우에만 셀을 특정 색상으로 변경하려는 경우 문제를 이해합니다.

    조건부 서식을 사용하여 완료할 셀 규칙을 강조 표시할 수 있습니다.

    예를 들어 "a b c d"가 포함된 데이터 열을 만들고, 영역을 선택하고, 조건부 서식을 선택한 >, 셀 규칙에 > 같음 강조 표시를 선택합니다.

    이미지

    셀 "a"의 값은 노란색으로, "b"는 녹색으로, "c"는 연한 빨간색으로 설정됩니다.

    이미지

    설정이 완료되면 "b"에서 "c"까지의 값이 셀을 밝은 빨간색으로 바꿉니다.

    이미지

    그러나 이 방법을 사용하려면 더 많은 양의 데이터를 처리할 때 더 많은 규칙을 설정해야 하므로 재량에 따라 사용하는 것이 좋습니다.

    이 작업을 수행하려면 VBA를 사용하여 셀 배경색에 따라 조건부 서식을 변경하는 함수를 만들 수 있습니다. 다음 VBA 코드에는 두 부분이 포함되어 있습니다.

    1. 셀 배경색을 기준으로 조건부 서식을 적용하는 기능입니다.
    2. 기존 조건부 서식을 "셀 텍스트 기준"에서 "셀 배경색 기준"으로 변환하는 함수입니다.

    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 코드 사용 지침

    1. Excel 통합 문서를 엽니다.
    2. Alt + F11 을 눌러 VBA 편집기를 엽니다.
    3. 모듈 삽입을 클릭하여 새 모듈을 삽입>.
    4. 제공된 VBA 코드를 복사하여 모듈에 붙여넣습니다.
    5. VBA 편집기를 닫습니다.
    6. Alt + F8을 누르고 함수(예: ApplyConditionalFormattingBasedOnBackgroundColor) 를 선택한 다음 실행을 클릭하여 원하는 함수를 실행합니다.

    이러한 함수는 예제에 지정된대로 셀 배경색을 기반으로 조건부 서식을 적용하고 변환합니다. 특정 사용 사례에 필요한 범위와 색상을 조정합니다.

    솔직히

    카를로스 - MSFT | Microsoft 커뮤니티 지원 전문가

    이 대답이 도움이 되었나요?

    댓글 0개 설명 없음