我正在嘗試將一些注釋和值放入一系列單元格中,如下所示:
With Range("B5:C8")
.AddComment "Current Sales"
End With
當我這樣做時,我得到一個錯誤運行時錯誤“5”:無效的程序呼叫或引數
當我嘗試這個時:
With Range("B5:C8")
.Value = 35
End With
它作業正常,AddComment 有什么不同的作業方式嗎?
我在 Windows 10 上使用 Excel 365
謝謝你的幫助
uj5u.com熱心網友回復:
要處理具有多個單元格的范圍,您將需要一個回圈。
此外,由于您不能添加多個評論,因此您需要先洗掉原始評論來替換它,或者您可以將新評論附加到舊評論。
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
uj5u.com熱心網友回復:
回圈遍歷每個單元格...
Dim objCell As Range
For Each objCell In Range("B5:C8")
objCell.AddComment "Current Sales"
Next
...省略了錯誤檢查。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/405379.html
標籤:
上一篇:跟蹤單元格更改的次數
