【问题标题】:Add a comment to a cell of a Listobject in Excel Listobject table向 Excel Listobject 表中 Listobject 的单元格添加注释
【发布时间】:2020-01-29 15:19:41
【问题描述】:

我在 excel 中有一个表格(listobject),我想在其中的某些单元格中添加 cmets。

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 方法,但它们不起作用。

有什么帮助吗?

谢谢

【问题讨论】:

  • iTem - 这就是你的词在​​ VBE 中的实际外观吗?大写已关闭。该代码对我有用。你有同名的变量吗?
  • 那里已经有评论了吗?
  • 我同意@SJR。我猜是同名的变量或过程或模块/类等?
  • 我不知道为什么 VBE 制作 iTem 而不是 Item。我搜索了“Dim ITem”并且没有这样的变量。但无论如何,足够奇怪这不是问题,因为 .iTem(1).Interior.color = vbGreen 有效,而所有其他时间我使用 .item 都不是问题。
  • 感谢您的发现。我将编辑问题

标签: excel vba listobject


【解决方案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

【讨论】:

  • 就是这样。显然 .addcomment myString 不会覆盖评论(如果有的话)。所以先删除它,然后再添加它。谢谢。
猜你喜欢
  • 2021-11-28
  • 2016-07-20
  • 2018-09-12
  • 2018-12-31
  • 2012-09-11
  • 2019-05-14
  • 2019-05-04
  • 1970-01-01
  • 2020-07-06
相关资源
最近更新 更多