获取types不匹配错误
这个VBA代码validation计算的单元格。 如果该值超过500.00美元或具有#N / A,则应为整行和粗体字着色。
但我不断得到这种Type Mismatch
错误:
Dim lngCounter As Long Dim Lastrow As Long Lastrow = ActiveSheet.Range("A65536").End(xlUp).Row For lngCounter = 3 To Lastrow With Cells(lngCounter, "J") If .Value >= 500 Or IsError(Cells) Then Cells(lngCounter, "I").Interior.ColorIndex = 44 Cells(lngCounter, "J").Interior.ColorIndex = 44 Rows(lngCounter).Font.Bold = True Else End If End With Next lngCounter
这行可能会引发错误:
If .Value >= 500 Or IsError(Cells) Then
这一行有两个错误。 Cells
在你的代码中是没有的。 你需要像Cells(lngCounter, "J")
等
此外,如果该单元格包含错误值,则在尝试评估该语句的前半部分时会出现“不匹配”错误,因为#N #N/A
或#DIV0!
等不是数字值,因此与数字运算符(如>=
一起使用时会出现types不匹配。
使用一些更多的variables,并微调一下代码。 试试这个(未经testing):
Sub Test() Dim lngCounter As Long Dim Lastrow As Long Dim myCell as Range Lastrow = ActiveSheet.Range("A65536").End(xlUp).Row For lngCounter = 3 To Lastrow Set myCell = Cells(lngCounter, "J") With myCell If IsError(.Value) Then FormatThis myCell ElseIf .Value >= 500 Then FormatThis myCell End If End With Next lngCounter End Sub Sub FormatThis(cl as Range) 'receives a cell in column J, use the OFFSET and RESIZE methods ' to return a range of cells in columns I & J to format color. cl.Offset(0,-1).Resize(1,2).Interior.ColorIndex = 44 Rows(cl.Row).Font.Bold = True End Sub
它是否必须是VBA? 您可以将此条件格式公式应用到行3:65536
=AND(OR($J3>=500,ISNA($J3)),$J3<>"")
您必须始终首先testing错误:
Sub dural() For Each r In Selection If IsError(r.Value) Then MsgBox r.Text Else If r.Value > 100 Then MsgBox r.Value End If End If Next r End Sub