해결 엑셀2016
특정 색상의 텍스트를 다른 색상으로 일괄 변경하는 방법
주어진 상황의 예시를 들자면 아래와 같습니다.

각 컬럼별 문자열 색상이 각각 달리 들어가 있어 Ctrl + H 를 통한 찾아 바꾸기 기능으로써 문자의 색상을 변경할 수는 없는 상태입니다. C와 D열은 마치 일괄적으로 변환이 가능한 것처럼 들어가 있으나 예시인 관계로 편의상 내용을 생략해 그렇고 실제로는 B열과 동일한 상황입니다.
현재 설정된 기존의 글자 색상을 기준으로 연녹색 → 진녹색, 하늘색 → 진청색 과 같은 형태로 기존 글자의 색상을 일괄적으로 변경하고자 합니다. 혹시 이를 수행 가능한 VBA 스크립트가 있을까요? 특정 문자열 내용을 기준으로 색상을 부여하는 기능은 많이 보이던데 이와 같은 상황에 대한 방안은 찾아봐도 없네요. 이렇게 작업해야 할 항목이 한두개면 직접 수정하고 끝내겠지만 이런 항목이 수백개에 달하는 상황이라 고수님들의 도움이 필요합니다.

각 컬럼별 문자열 색상이 각각 달리 들어가 있어 Ctrl + H 를 통한 찾아 바꾸기 기능으로써 문자의 색상을 변경할 수는 없는 상태입니다. C와 D열은 마치 일괄적으로 변환이 가능한 것처럼 들어가 있으나 예시인 관계로 편의상 내용을 생략해 그렇고 실제로는 B열과 동일한 상황입니다.
현재 설정된 기존의 글자 색상을 기준으로 연녹색 → 진녹색, 하늘색 → 진청색 과 같은 형태로 기존 글자의 색상을 일괄적으로 변경하고자 합니다. 혹시 이를 수행 가능한 VBA 스크립트가 있을까요? 특정 문자열 내용을 기준으로 색상을 부여하는 기능은 많이 보이던데 이와 같은 상황에 대한 방안은 찾아봐도 없네요. 이렇게 작업해야 할 항목이 한두개면 직접 수정하고 끝내겠지만 이런 항목이 수백개에 달하는 상황이라 고수님들의 도움이 필요합니다.
프

댓글 10
Sub FontColorPartOfText() 'Source: https://powerspreadsheets.com/ 'More information: https://powerspreadsheets.com/vba-font-color-part/ 'Declare the following: 'Step 1: Object variable to represent cell where you change the font color for part of the text 'Variable to represent: 'Step 2: Full text (with the substring whose font color you change) 'Step 3: The text portion (substring) whose font color you change 'Step 4: Number of characters in text portion (substring) whose font color you change 'Step 2: Loop iteration variable Dim MyCell As Range Dim MyFullText As String Dim MyTextToChangeColor As String Dim MyTextToChangeColorLength As Long Dim iCounter As Long 'Step 1: Assign object reference (to cell A6 in the "Excel VBA Font Color Part Text" worksheet in this workbook) to MyCell object variable Set MyCell = ThisWorkbook.Worksheets("Excel VBA Font Color Part Text").Range("A6") 'Step 2: Assign string stored in cell represented by MyCell object variable to MyFullText variable MyFullText = MyCell.Value 'Step 4: Assign string with text whose color you change to MyTextToChangeColor variable MyTextToChangeColor = "text" 'Step 4: Assign number of characters in string represented by MyTextToChangeColor variable to MyTextToChangeColorLength variable MyTextToChangeColorLength = Len(MyTextToChangeColor) 'Step 2: Loop from 1 to the number of characters in the string represented by MyFullText variable For iCounter = 1 To Len(MyFullText) 'Do the following: 'Step 4: Extract a helper string from the string represented by the MyFullText variable 'Starting at the position equal to the current loop iteration 'With a length equal to the number represented by the MyTextToChangeColorLength variable 'Step 5: Test whether the helper string is equal to the string represented by the MyTextToChangeColor variable 'Step 6: If the helper string is equal to the string represented by the MyTextToChangeColor variable, change the text portion (substring) whose font color you change If Mid(MyFullText, iCounter, MyTextToChangeColorLength) = MyTextToChangeColor Then 'Step 7: Identify the part of text whose font color you change 'Step 8: Use the RGB function to set the value of the Font.Color property. Specify the red, green, and blue components of the color as follows: 'Red: 0 'Green: 128 'Blue: 55 MyCell.Characters(iCounter, MyTextToChangeColorLength).Font.Color = RGB(0, 128, 55) End If Next iCounter End Sub여기 참고해보세요~
실제 변경하려는 색을 Color1/2 -> 3/4에 반영해 주시면 됩니다.
색상수에 따라 Color1.2.3.4 부분이 있는 줄인 지정부분과 변경부분만 수정하면 됩니다.
스크립트 적용 전과 후의 파일을 첨부해 드립니다. 혹시 해당 현상이 발생하는 원인을 알 수 있을까요?
캐바캐라서 Excel의 bug인지도 모르겠습니다.
아래와 같이 개별이 아니라 연속된 글자색을 한 번에 변경하도록 모듈화 하여 호출하는 방식으로 변경해 봤습니다.
일단, 이렇게 적용할 경우 이상한 현상은 발생하지 않았습니다.
첨부 파일 참고하세요.
Sub CharacterFontColorChange(C As Range, FromC As Long, ToC As Long) Dim iCnt As Long, iPos As Long, iLen As Long, i As Long With C iCnt = 0 iCnt = .Characters.Count If iCnt > 0 Then iPos = 0: iLen = 0: i = 0 Do While True i = i + 1 If i > iCnt Then Exit Do If .Characters(i, 1).Font.Color = FromC Then If iPos = 0 Then iPos = i: iLen = iLen + 1 Else iLen = iLen + 1 Else If iPos > 0 Then .Characters(iPos, iLen).Font.Color = ToC: iPos = 0: iLen = 0 End If Loop End If End With End Sub