我有一堆排列包含文本,如:InStr函數搜索和逗號細胞
dog,cat,mouse
bat,dog,fly
fish,beaver,horse
我試圖尋找並突出顯示包含特定單詞的行:
Public Sub MarkDuplicates()
Dim iWarnColor As Integer
Dim rng As Range
Dim rngCell As Variant
Dim LR As Long
Dim vVal
Dim tRow
LR = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("B1:B" & LR)
iWarnColor = xlThemeColorAccent2
For Each rngCell In rng.Cells
tRow = rngCell.Row
If InStr(rngCell.Value, "dog") = 1 Then
rngCell.Interior.ColorIndex = iWarnColor
Else
rngCell.Interior.Pattern = xlNone
End If
Next
End Sub
只要單詞'dog'是逗號字符串中的第一個單詞,所以它會突出顯示第一行,但不會顯示第二行,因爲單詞'dog'出現在'bat' 。我是否需要先刪除逗號或者有沒有更好的方法來做到這一點?
您是否嘗試'.Find'? http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/如果行數很多,循環遍歷列中的每一行將非常緩慢。當使用'.Find'時,使用'LookAt:= xlPart' – 2013-04-11 19:18:37
你也可以使用Autofilter來達到你想要的。 – 2013-04-11 19:22:09
或簡單地使用像這樣的比較'如果InStr(1,rngCell.Value,「dog」)<> 0 then'...此外,檢查'InStr'函數的參數。 – 2013-04-11 19:31:00