我目前在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?
....
我想要做的是将红色的Joes消息和蓝色的Jims消息着色。谁能帮我理解怎么做?
我需要一些代码来遍历我的工作表并尝试将文本与已知的起始字符串匹配,然后相应地更改其颜色。我对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
答案 0 :(得分:2)
进入单元格A2,单击“条件格式”,然后单击“新规则”并选择“使用公式确定要格式化的单元格”
输入
=FIND("Joe Smith",$A1)>0
点击格式并设置字体>颜色为蓝色
在单元格A2上重做完全相同的内容并输入
=FIND("Jim Bob",$A1)>0
点击格式并设置字体>颜色为红色
然后转到条件格式>管理规则并在“适用于”只需输入“$ A:$ A”
答案 1 :(得分:2)
非常直接。这可能是您为消息着色的简单机制
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
这会为单元格中的文本着色
用于将整个单元格背景着色,将c.Offset(1, 0).Font.Color = vbRed
替换为c.Offset(1, 0).Interior.Color = vbRed
,只需要添加,如果您想要更改整个ROW,请添加c.offset(1,0).EntireRow.Interior.Color
答案 2 :(得分:1)
此代码未经过测试,但它至少可以让您了解自己需要做什么。
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