添加注释在与范围( "F15" ) 一起使用时不起作用



我正试图将一些注释和值放入一系列单元格中,如下所示:

With Range("B5:C8")
.AddComment "Current Sales"
End With

当我这样做时,我得到一个错误运行时错误";5〃:无效的过程调用或参数

当我尝试这个:

With Range("B5:C8")
.Value = 35
End With

它工作得很好,AddComment有什么不同的工作方式吗?

我在Windows 10 上使用Excel 365

感谢的帮助

要处理具有多个单元格的范围,需要一个循环。

此外,由于不能添加多个注释,您需要先删除原始注释来替换它,或者可以将新注释附加到旧注释中。

Sub AddCellComment(FullRange As Range, cmt As String, Optional Replace As Boolean = False)
Dim s As String
Dim r As Range

For Each r In FullRange
If r.Comment Is Nothing Then
r.AddComment cmt
Else
' (cannot set .Text directly)
' save original comment into a variable
s = r.Comment.Text
' delete original comment
r.Comment.Delete

If Replace Then
' replace original comment
r.AddComment cmt
Else
' append new comment
r.AddComment s & vbCrLf & cmt
End If

End If
Next
End Sub

用法

Sub test()
AddCellComment Range("B5:C8"), "test"
' also works with F15 (per your subject)
AddCellComment Range("F15"), "test"
' to replace, add True as another argument
AddCellComment Range("B5:C8"), "test", True
End Sub

循环遍历每个单元格。。。

Dim objCell As Range
For Each objCell In Range("B5:C8")
objCell.AddComment "Current Sales"
Next

错误检查已提交。

最新更新