将Excel电子表格单元格中的多色文本解析为多个单元格?

时间:2017-03-03 21:16:13

标签: excel excel-vba vba

我在单元格中有彩色文本字符序列。我想根据颜色将其解析为多个单元格,如下所示。颜色在重复。我尝试使用本论坛中已发布的一些解决方案,包括解决方案:How to extract text based on font color from a cell with text of multiple colors and separate multiple words by Delimiter?。但是,无法实现我想要的结果。有什么建议吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

这看起来是正确的。

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