Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

excel - How to extract text based on font color from a cell with text of multiple colors

I have a column of data (A). The data in each cell in column (A) is half one color and half another color. For example, let's say the first portion of the character string is red and the second portion of the character string is black. The length of the red and black character strings within each cell varies with no pattern. The type of characters that are red and black vary with no pattern. There is no space or special character that separates the red characters from the black characters within each cell. I would like to extract and copy the red characters from each cell into a new column (B) using a formula or function. Suggestions?

(A) Original..........(B) Red

abjksglkjaf..........abjk

kjd3kdn9j............kjd3kd

2hn89dslkjh..........2hn

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this user defined function:

Function redPart(x As Range) As String
    Dim res As String
    With x
        For i = 1 To Len(.Value)
            ' red = RGB(255, 0, 0)
            If .Characters(i, 1).Font.Color = RGB(255, 0, 0) Then
                res = res & .Characters(i, 1).Text
            End If
        Next
    End With
    redPart = res
End Function

just write in cell B1 formula =redPart(A1) and drag it down.

Result:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...