VBA:错误13,类型不匹配

时间:2015-08-24 19:39:32

标签: vba runtime-error

我想让Excel自动更改单元格的颜色,如果该单元格的值比同一行但左边三列的单元格大或小10%。

以下是代码:

Sub testing1()
    Dim x As Integer
    Dim y As Integer

    For x = 35 To 5 Step -3
        For y = 11 To 75 Step 1
            If Cells(y, x).Value < 0.9 * Cells(y, x - 3).Value Or _
               Cells(y, x).Value > 1.1 * Cells(y, x - 3) Then
                Cells(y, x).Interior.ColorIndex = 22
            End If
        Next y
    Next x
End Sub

在我定义y之前,我使用了一行,如11。它是这样的:cells(11,x).value;代码运行没有问题。

但是,当我从数字更改为变量y时,我收到错误:Error 13, type mismatch

此错误的来源是什么?

1 个答案:

答案 0 :(得分:1)

再添加一个条件图层。实际上,如果你的单元格中包含a并且你将它乘以5,那么通常会出现类型不匹配错误。

If IsNumeric(Cells(y, x)) Then
    If Cells(y, x).Value < 0.9 * Cells(y, x - 3).Value Or Cells(y, x).Value > 1.1 * Cells(y, x - 3) Then

        Cells(y, x).Interior.ColorIndex = 22

    End If
End If