我在excel中有一个表(列表对象),我想在其中的某些单元格中添加注释。
Dim infotbl As listobject
Set infotbl = ThisWorkbook.Sheets("index").ListObjects("infotbl")
Dim myString As String
myString = "Whatever"
' this line of code works:
infotbl.ListColumns(2).DataBodyRange.item(1).Interior.color = vbGreen
' one of the next two lines of code does not work:
infotbl.ListColumns(2).DataBodyRange.item(1).AddComment
infotbl.ListColumns(2).DataBodyRange.item(1).Comment.Text Text:=myString
错误为运行错误1004 应用程序定义或对象定义的错误。
我检查了stackoverflow中的几个帖子,这些帖子中有.AddComment和.comment.Text方法,但它们不起作用。
有帮助吗?
谢谢
答案 0 :(得分:1)
以下代码对我有用:
Sub TestTableComment()
Dim infotbl As ListObject: Set infotbl = ThisWorkbook.Sheets("index").ListObjects("infotbl")
Dim myString As String: myString = "Whatever"
With infotbl.ListColumns(2).DataBodyRange
.Item(1).Interior.Color = vbGreen
.Item(1).ClearComments
.Item(1).AddComment myString
End With
End Sub