【发布时间】:2019-10-06 01:35:36
【问题描述】:
B 列有许多包含“IS”和“S”的单元格。第一个单元格 B4 包含“IS”,第一个单元格 B6 包含“S”,都用颜色 37 填充。下面的代码:B4="IS1" 为真到1st if 并着色; B5="IS2" 是真到2nd if 和着色,但只想要第一个包含“IS”的单元格着色。请帮助提供以下代码。
Dim selectedCells As Range, cell As Range
Set selectedCells = Range("B3:B38")
'B4 = "IS1"
'B5 = "IS2"
'B6 = "S06"
'B7 = "S07" etc
Dim iproColoured As Boolean, soloColoured As Boolean
iproColoured = False
soloColoured = False
For Each cell In selectedCells
If Application.IsNumber(Application.Search("IS", cell)) And iproColoured = False Then
cell.Interior.ColorIndex = 37
iproColoured = True
End If
If Application.IsNumber(Application.Search("S", cell)) And soloColoured = False Then
cell.Interior.ColorIndex = 37
soloColoured = True
End If
Next cell
【问题讨论】:
-
Exit For也许 - 虽然你仍然希望B5被着色,因为它满足第二个If? -
@BigBen 我只希望第一个包含“IS”(B4)的单元格被着色。是的,如果我不希望它被着色,B5 满足第二个。这就是问题所在,如何解决这个问题。
-
在第一个
If...End If中添加Exit For。 -
@BigBen 我不知道退出。请输入有关如何根据您的建议修复它的更多详细信息
-
@BigBen 我用谷歌搜索了
Exit For。在1st if之后无法退出for 循环,因为我需要执行2nd if以在包含“S”的第一个单元格中着色。这就是我花时间绑定2nd if的原因。