我正在尝试创建一个宏来检查列的特定值。 我试了一下手。它检查特定值,但问题是它只检查单个值。
例如 - 假设有三个单元格。
Cell 1 contains Red
Cell 2 contains Black
Cell 3 contains Red;Black
运行宏后,Cell 3会突出显示。
虽然我已经在数组中添加了两种颜色,但它正在检查完全匹配。
Cells.Find(What:="Color Filter", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Dim myarray As Variant
Dim r As Variant
myarray = Array("Beige", "Black", "Blue", "Brown", "Cream", "Green", "Grey", "Maroon", "Multi", "Orange", "Pink", "Purple", "Red", "Rust", "Turquoise", "Violet", "White", "Wine", "Yellow", "Transparent")
Dim Rng As Range
Dim Dn As Range
Set Rng = Range(Cells(MyRow, MyCol), Cells(LastRow, MyCol))
For Each Dn In Rng
For Each r In myarray
If Dn = r Then
Dn.Interior.ColorIndex = xlNone
Exit For
ElseIf Dn = "" Then
Dn.Interior.ColorIndex = 3
Else
Dn.Interior.ColorIndex = 3
End If
Next r
Next Dn
答案 0 :(得分:0)
您可以使用Instr
函数根据单元格中的值来评估数组的每个成员。这将在单元格值的任何位置查找每种颜色。
我稍微重构了你的代码,这样就可以了(我认为)保持原来的意图。
Dim myarray As Variant, r As Variant
myarray = Array("Beige", "Black", "Blue", "Brown", "Cream", "Green", "Grey", "Maroon", "Multi", "Orange", "Pink", "Purple", "Red", "Rust", "Turquoise", "Violet", "White", "Wine", "Yellow", "Transparent")
Dim Rng As Range, Dn As Range
Set Rng = Range(Cells(MyRow, MyCol), Cells(LastRow, MyCol))
For Each Dn In Rng
If Dn = "" Then
Dn.Interior.ColorIndex = 3
Else
For Each r In myarray
If InStr(r, Dn.Value2) Then
Dn.Interior.ColorIndex = xlNone
Exit For
End If
Next r
End If
Next Dn
答案 1 :(得分:0)
split
,然后创建一个循环...这将以某种方式允许您实际使用每种颜色的信息。因此,在单元格包含Red;Black
的情况下,它将对第一部分进行一次评估,对第二部分进行第二次评估。
试试这样:
Dim myarray As Variant, r,cs,CutString As Variant
myarray = Array("Beige", "Black", "Blue", "Brown", "Cream", "Green", "Grey", "Maroon", "Multi", "Orange", "Pink", "Purple", "Red", "Rust", "Turquoise", "Violet", "White", "Wine", "Yellow", "Transparent")
Dim Rng As Range, Dn As Range
Set Rng = Range(Cells(MyRow, MyCol), Cells(LastRow, MyCol))
For Each Dn In Rng
If Dn = "" Then
Dn.Interior.ColorIndex = 3
Else
For Each r In myarray
CutString = Split(Dn.Value2, ";")
For Each cs In CutString
If cs = r Then
Dn.Interior.ColorIndex = xlNone
Exit For
End If
Next cs
Next r
End If
Next Dn