[英]How to extract text based on font color from a cell with text of multiple colors and separate multiple words by Delimiter?
How to extract text based on font color from a cell with text of multiple colors 如何从具有多种颜色的文本的单元格中基于字体颜色提取文本
I have a column of data (A). 我有一列数据(A)。 The data in each cell in column (A) is half one color and half another color.
(A)列中每个单元格中的数据是一种颜色的一半,另一种颜色的一半。 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.
我尝试了上述链接中的解决方案,但由于我是vba的初学者,因此无法进行更改以适合我的目的。 please suggest methods to sole this problem.
请提出解决此问题的方法。
(A) Original..........(B) Red (A)原始..........(B)红色
abcdefgh..........abc, gh abcdefgh .......... abc,gh
A User Defined Function (aka UDF) should get you through this. 用户定义的函数(又名UDF)应该可以帮助您完成此任务。
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
. 考虑了所有可用的红色阴影后,我选择了
.ColorIndex = 3
的更简单解决方案。 There is an optional parameter so that you can set your own ColorIndex property number. 有一个可选参数,因此您可以设置自己的ColorIndex属性编号。 If you need more colors, it should be a small matter to swap the code over to the .Font.Color property .
如果需要更多颜色,将代码交换到.Font.Color属性应该是一件小事。
The syntax used in the above image in B2 is, 上图中B2中使用的语法是:
=udf_Whats_Colored(A2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.