键入不匹配,但功能代码

时间:2013-05-13 08:44:56

标签: excel vba

此宏用于识别任何单元格中的字符串“%”,如果存在,则通过将其着色为黄色来识别它。

有趣的是它确实有效,但我一直在线上特别注意到类型不匹配错误:

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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分: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