Excel根据上面的string应用颜色

我目前在excel中有一个消息线程:

Cell A1 Joe SmithSunday 16th December Cell A2 Hey it's Joe Cell A3 Jim BobSunday 16th December Cell A4 Hi Jim! Cell A5 Jim BobMonday 17th Decembver Cell A6 How are you? .... 

我想要做的是颜色乔斯消息红色和吉姆消息蓝色。 任何人都可以帮助我了解如何做到这一点?

我需要一些代码循环通过我的工作表,并尝试匹配文本与已知的起始string,然后相应地更改其颜色。 我对VBA不太好,但是这里有一些sudo代码:

  if string starts with "Joe Smith" turn text colour of row below red else if string starts with "Jim Bob" turn text colour of row below blue else skip 

挺直的。 这可能是您为消息着色的简单机制

 Sub SimpleColoringMechanism() Dim c As Range ' loop through all cells in range A1:A + last userd Row in column A For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) ' when Joe Smith is found in cell's content If InStr(1, c.Text, "Joe Smith", vbTextCompare) > 0 Then ' color the message in red c.Offset(1, 0).Font.Color = vbRed ' when Jim Bob is found in cell's content ElseIf InStr(1, c, "Jim Bob", vbTextCompare) > 0 Then ' color the message in blue c.Offset(1, 0).Font.Color = vbBlue End If Next End Sub 

这使单元格中的文本变为颜色

为整个单元格背景着色replacec.Offset(1, 0).Font.Color = vbRedc.Offset(1, 0).Interior.Color = vbRed和只是添加,如果你想整个行改变添加一个c.offset(1,0).EntireRow.Interior.Color

进入单元格A2,单击“条件格式”,然后单击“新build规则”,然后select“使用公式确定要格式化的单元格”

input

 =FIND("Joe Smith",$A1)>0 

点击格式并将字体>颜色设置为蓝色

重做单元格A2上完全相同的东西,然后input

 =FIND("Jim Bob",$A1)>0 

点击格式并将字体>颜色设置为红色

然后转到条件格式>pipe理规则,并在“适用于”只是把“$ A:$ A”

这段代码没有经过testing,但至less应该让你知道你需要做什么。

 Dim iterator as integer Dim columnCount as integer With ActiveSheet columnCount = UsedRange.Rows.Count For iterator = 1 to columnCount If .Cells(iterator, 1) Like "Joe Smith*" Then .Cells(iterator + 1, 1).Interior.ColorIndex = 3 ElseIf .Cells(iterator, 1) Like "Jim Bob*" Then .Cells(iterator + 1, 1).Interior.ColorIndex = 4 End If Next End With 

在这里和这里支持参考。