Excel VBA If Cells.Comment fail



再次提出关于注释的问题。我想检查一个单元格是否包含某个注释值。运行此脚本时出现错误:

For AA = 0 To 200
For BB = 0 To 200
If Worksheets("Config IO").Range("D7").offset(0, 0).Value = "1" Then
If Worksheets("Config Algemeen").Cells(2 + AA, 9 + BB).Comment = "DI" Then
Aantal_DI = Aantal_DI + 1
Else
'Nothing
End If
Else
'Nothing
End If
Next BB
Next AA

我不知道这是怎么回事。我收到这样的消息:对象不支持此属性或方法。此错误出现在第二条"IF"线上。

这会在尝试引用注释中的文本之前检查注释是否存在。

  • 我已经将第一个IF检查移到循环之外,因为它每次都引用同一个单元格
  • 我还去掉了Offset(0,0),因为它偏移了0行0列——与原始地址相同
  • 我还去掉了.Value,因为这是范围的默认属性

Sub Test()
Dim AA As Long, BB As Long
If Worksheets("Config IO").Range("D7") = "1" Then
For AA = 0 To 200
For BB = 0 To 200
With Worksheets("Config Algemeen").Cells(2 + AA, 9 + BB)
If Not .Comment Is Nothing Then
If .Comment.Text = "DI" Then
Aantal_DI = Aantal_DI + 1
End If
End If
End With
Next BB
Next AA
End If
End Sub

替换:

If Worksheets("Config Algemeen").Cells(2 + AA, 9 + BB).Comment = "DI" Then

带有:

If Worksheets("Config Algemeen").Cells(2 + AA, 9 + BB).Comment.Text = "DI" Then

最新更新