types在For循环中的If语句中不匹配

我有很多麻烦试图找出为什么下面的代码给我一个Error '13' 。 难道我做错了什么?

 Sub summary() Dim last As Variant lastrow = Sheet4.Range("g" & Rows.Count).End(xlUp).Row c = 0 For x = 2 To lastrow If Sheet4.Cells(x, 10) = Sheet4.Cells(x, 12) Then c = c + 1 End If Next x End Sub 

检查Col 10或Col 12中的一个单元格。它们有一个公式错误。 #DIV/0! #NA#DIV/0! 或别的东西,因此也许这就是你得到Run Time Error 13 Type Mismatch Error

检查错误发生时x的值是哪个单元格的最好方法。

这是一个复制问题的例子

A1单元格中input=0/0并运行这段代码。

在这里输入图像说明

编辑要find问题可能在哪一行,请尝试这个简单的事情。

 Sub summary() Dim lastrow As Long Dim c As Long, x As Long On Error GoTo Whoa With Sheet4 lastrow = .Range("g" & .Rows.Count).End(xlUp).Row c = 0 For x = 2 To lastrow If .Cells(x, 10) = .Cells(x, 12) Then c = c + 1 End If Next x End With Exit Sub Whoa: MsgBox "At the time of error the value of x is " & x End Sub 

尝试

 Sub summary() Dim lastrow As Variant dim c as integer lastrow = sheets("Sheet4").Range("g" & Rows.Count).End(xlUp).Row c = 0 For x = 2 To lastrow If sheets("Sheet4").Cells(x, 10) = sheets("Sheet4").Cells(x, 12) Then c = c + 1 End If Next x end sub