我的Excel工作表如下所示:
+=========+=========+=================================+======================================+
| MPN | BRAND | TITLE | URL |
+=========+=========+=================================+======================================+
| GB38905 | GRIFFIN | All-Terrain Case for iPhone ... | https://www.example.com/gb38905.html |
+---------+---------+---------------------------------+--------------------------------------+
..我需要根据这些条件(所有不区分大小写)突出显示行的背景为绿色|橙色|红色:
这是我第一次尝试使用VBA:
Option Compare Text
Sub MySub()
Dim rng As Range
Dim row As Range
Dim cell As Range
Set rng = Range("A1: E17361")
For Each row In rng.Rows
For Each cell In row.Cells
MPN = Range("C1").Value
If InStr(Range("C3").Value, MPN) And InStr(Range("C4").Value, MPN) > 0 Then
cell.Interior.Color = vbGreen
ElseIf InStr(Range("C3").Value, MPN) Or InStr(Range("C4").Value, MPN) > 0 Then
cell.Interior.Color = vbOrange
Else
cell.Interior.Color = vbYellow
End If
Next cell
Next row
End Sub
..它显然不起作用。它将所有行都变为绿色。
答案 0 :(得分:1)
Option Compare Text
Sub MySub()
Dim rng As Range
Dim row As Range
Dim MPN, u As Boolean, t As Boolean, clr as long
Set rng = Range("A1:E17361")
For Each row In rng.Rows
MPN = row.Cells(1).Value
u = InStr(row.cells(4).Value, MPN) > 0
t = instr(row.cells(3).Value, MPN) > 0
If u And t Then
clr = vbGreen
ElseIf u Or t Then
clr = vbMagenta
Else
clr = vbYellow
End If
row.Interior.Color = clr
Next row
End Sub