如何从带有多种颜色的文本并通过Delimiter分隔多个单词的单元格中基于字体颜色提取文本? [英] How to extract text based on font color from a cell with text of multiple colors and separate multiple words by Delimiter?

查看:101
本文介绍了如何从带有多种颜色的文本并通过Delimiter分隔多个单词的单元格中基于字体颜色提取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



上图中B2中使用的语法是

  = udf_Whats_Colored(A2)








sup> ColorIndex调色板表


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. I have to extract each word separated by delimiter if they are at different places. I tried the solution in the above link but unable to make changes to suit my purpose as i am a beginner for vba. please suggest methods to sole this problem.

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

abcdefgh..........abc, gh

解决方案

A User Defined Function (aka UDF) should get you through this.

Function udf_Whats_Colored(rTXT As Range, Optional iCLRNDX As Long = 3)
    Dim c As Long, str As String

    For c = 1 To Len(rTXT.Text)
        With rTXT.Characters(Start:=c, Length:=1)
            If .Font.ColorIndex = iCLRNDX Then
                If Not CBool(Len(str)) Or _
                   rTXT.Characters(Start:=c + (c > 1), Length:=1).Font.ColorIndex = iCLRNDX Then
                    str = str & Mid(rTXT.Text, c, 1)
                Else
                    str = str & ", " & Mid(rTXT.Text, c, 1)
                End If
            End If
        End With
    Next c
    udf_Whats_Colored = str

End Function

After considering all of the shades of red that are available, I opted for the simpler solution of .ColorIndex = 3. There is an optional parameter so that you can set your own ColorIndex property number. If you need more colors, it should be a small matter to swap the code over to the .Font.Color property.

        

The syntax used in the above image in B2 is,

=udf_Whats_Colored(A2)


        
                     ColorIndex Palette Table

这篇关于如何从带有多种颜色的文本并通过Delimiter分隔多个单词的单元格中基于字体颜色提取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆