1

This VBA code verifies calculated cells. If the value is over $500.00 or has #N/A, then it should color the entire row and bold font.

But I keep getting this Type Mismatch error:

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
4

3 回答 3

2

This line is probably raising the error:

If .Value >= 500 Or IsError(Cells) Then

There are two errors in that line. Cells is nothing in your code. you would need like Cells(lngCounter, "J") etc.

Also, if that cell contains an error value, you will get the Mismatch error when trying to evaluate the first half of that statment, because #N/A or #DIV0! etc are not numeric values and therefore present a type Mismatch when used with numeric operators like >=.

Use some more variables, and fine-tune the code a bit. Try this (untested):

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
于 2013-08-12T17:20:04.717 回答
1

Does it have to be VBA? You could apply this conditional format formula to rows 3:65536

=AND(OR($J3>=500,ISNA($J3)),$J3<>"")
于 2013-08-12T17:15:23.907 回答
1

You must always test for the error first:

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
于 2013-08-12T17:20:14.470 回答