Excel VBA 代码中的错误

富兰克林·瓦吉斯

我的代码有问题,编译错误“类型不匹配”。特别是在以下行中

If comp.Cells(r, c) = "" Then

我尝试用一​​个数字分别切换 r 和 c 并且它有效,但不能一起使用。这是完整的代码

Sub missing_data()

Dim comp As Worksheet
Set comp = Sheets("Compiled")
empty_cells = 0
sixten_comp = 0
    For r = 2 To 103

        For c = 5 To 18
           'searching for empty cells 
            If comp.Cells(r, c) = "" Then
                If (c + 1998) > comp.Cells(r, 3) Then
                    empty_cells = empty_cells + 1
                    comp.Cells(r, c).Interior.ColorIndex = 13

                    'If comp.Cells(r, 1).Interior.ColorIndex = 1 Then sixten_comp = sixten_comp + 1

                End If
            End If

        Next c

    Next r

MsgBox "Number of total cells left is " & empty_cells
'MsgBox "Number of total cells left in 16 companies is " & sixten_comp

End Sub
Subodh Tiwari sktneer

您正在检查的单元格可能包含由公式返回的错误,因此添加另一个条件以检查单元格是否不包含错误,如果没有,请继续检查其他条件....

If Not IsError(comp.Cells(r, c)) Then
    If comp.Cells(r, c) = "" Then
        If (c + 1998) > comp.Cells(r, 3) Then
            empty_cells = empty_cells + 1
            comp.Cells(r, c).Interior.ColorIndex = 13
            'If comp.Cells(r, 1).Interior.ColorIndex = 1 Then sixten_comp = sixten_comp + 1
        End If
    End If
End If

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章