简体   繁体   English

Excel VBA代码中的错误

[英]Error in Excel VBA code

I am having a problem with my code, a compile error "type mismatch". 我的代码有问题,编译错误“类型不匹配”。 Especially in the following line 特别是在以下一行

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

i tried switching r and c with a number separately and it worked, but not together. 我尝试分别用数字切换r和c,并且有效,但不一起使用。 Here's the full code 这是完整的代码

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

The cells you are checking may contain an error returned by a formula so add another condition to check if the cell doesn't contain an error and if not, proceed to check other conditions.... 您正在检查的单元格可能包含由公式返回的错误,因此添加另一个条件以检查该单元格是否不包含错误;如果不包含,请继续检查其他条件。

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

Try: 尝试:

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

instead of: 代替:

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

it should work without a problem 它应该可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM