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
152 views
in Technique[技术] by (71.8m points)

vba - Parsing multicolored text in Excel spreadsheet cell into multiple cells?

I have sequence of colored text characters in a cell. I would like to parse it based on color to multiple cells as shown below. The colors are repeating. I tried to use some of the solutions already posted in this forum including the solution: How to extract text based on font color from a cell with text of multiple colors and separate multiple words by Delimiter?. But, could not achieve the results I want. Any suggestions?

enter image description here

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This looks right.

Option Explicit

Function udf_Color_Piece(rTXT As Range, Optional iNDX As Long = 1)
    Dim c As Long, seg As Long, clr As Long

    seg = 0
    clr = -9
    udf_Color_Piece = vbNullString

    For c = 1 To Len(rTXT.Text)
        With rTXT.Characters(Start:=c, Length:=1)
            If clr <> .Font.Color Then
                seg = seg + 1
                clr = .Font.Color
                If seg > iNDX Then Exit Function
            End If
            If seg = iNDX Then
                udf_Color_Piece = udf_Color_Piece & .Text
            End If
        End With
    Next c

End Function

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

...