2013-05-13 114 views
1

該宏用於識別任何單元格中的字符串「%」,如果存在,則通過將它着色爲黃色來標識它。類型不匹配,但功能代碼

有趣的是它不實際工作,但我不斷收到類型不匹配錯誤後記專門就行了:

If InStr(rngCell.Value, "%") > 0 Then 

這是我下面全碼:

Public Sub Markerrorvalues() 

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("C1:C" & LR) 
iWarnColor = xlThemeColorAccent2 

For Each rngCell In rng.Cells 
    tRow = rngCell.Row 
    If InStr(rngCell.Value, "%") > 0 Then 
     rngCell.Interior.ColorIndex = iWarnColor 
    Else 
     rngCell.Interior.Pattern = xlNone 
    End If 
Next 

End Sub 

任何幫助,將不勝感激!

+0

試圖複製,但...它運行順利,沒有錯誤:/當你得到錯誤? – Davide 2013-05-13 08:49:09

+1

是的,我也按照預期工作。你能給我們一個你運行它的數據的例子嗎? – 2013-05-13 08:54:48

+0

這似乎確實工作,如果我刪除一定量的行下..也許在C列有一些不愉快的運行線:如果InStr(rngCell.Value,「%」)> 0然後 – user1717622 2013-05-13 09:09:13

回答

1

也許你有一些細胞誤差值(例如#REF!#DIV/0!等)

過濾這些了,包裹裏面Not IsError有條件的麻煩代碼:

If Not IsError(rngCell.Value) Then 
    If InStr(rngCell.Value, "%") > 0 Then 
     rngCell.Interior.ColorIndex = iWarnColor 
    Else 
     rngCell.Interior.Pattern = xlNone 
    End If 
EndIf 
+0

你能擴展嗎?仍然不確定這個嵌套語法。 – user1717622 2013-05-13 11:16:24

+1

@ user1717622:chris'answer is right;我爲你澄清了一點。 – 2013-05-13 13:06:20