Excel VBA 代码错误
Error in Excel VBA code
我的代码有问题,编译错误 "type mismatch"。特别是下面一行
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
尝试:
If comp.Cells(r, c).Value = "" Then
而不是:
If comp.Cells(r, c) = "" Then
它应该可以正常工作
您正在检查的单元格可能包含公式返回的错误,因此添加另一个条件以检查单元格是否不包含错误,如果没有,请继续检查其他条件....
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
我的代码有问题,编译错误 "type mismatch"。特别是下面一行
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
尝试:
If comp.Cells(r, c).Value = "" Then
而不是:
If comp.Cells(r, c) = "" Then
它应该可以正常工作
您正在检查的单元格可能包含公式返回的错误,因此添加另一个条件以检查单元格是否不包含错误,如果没有,请继续检查其他条件....
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