首页 > 解决方案 > 检查线程注释

问题描述

我的办公室刚刚升级到新版本的 Office 365,随之而来的是一种完成评论的新方式。旧评论现在称为“注释”,新评论现在称为“评论”。

在我的一个工作簿中,我有一个按钮,单击该按钮将查看某些单元格以检查是否有评论。然后它将根据找到的内容为单元格着色。

(我的按钮的完整代码发布在下面)第 9 行包含问题

在早期版本的 Excel 中,此按钮可以正常工作。但是,现在它仅在单元格有“注释”时才有效,如果它们有“评论”则不起作用。在我的代码中,我一直使用的类称为“Comment”,所以我的代码类似于“If Cells(row, col).Comment Is Nothing...”。这个类仍然有效,但只寻找笔记。我查看了对象库并在隐藏对象下找到了一个名为“CommentThreaded”的新类。我尝试将我的代码更改为(“如果 Cells(row, col).CommentThreaded Is Nothing...”),但它不起作用。当我单击该按钮时,我现在得到一个运行时错误:当它尝试访问这个新类时出现应用程序定义或对象定义错误。

有谁知道我需要更改什么才能让我的按钮与线程评论一起使用?

谢谢,迈克

Sub Comments()
Dim xrow As Integer
Dim xcol As Integer

For xrow = 7 To 88
    For xcol = 3 To 15
        If Cells(xrow, xcol).Value <= -0.1 Or Cells(xrow, xcol).Value >= 0.1 Then
            If Cells(5, xcol).Value = "MTD %" Or Cells(5, xcol).Value = "YTD %" Then
                If Not Cells(xrow, xcol).Comment Is Nothing Then
                    Cells(xrow, xcol).Interior.Color = RGB(155, 255, 188)
                Else
                    Cells(xrow, xcol).Interior.Color = RGB(255, 255, 0)
                End If
            End If
        End If
    Next xcol
Next xrow
End Sub

标签: excelvba

解决方案


截至 2019 年 5 月 15 日,新对象CommentThreadedMicrosoft描述。
在我的 Excel 版本 1906 中,VBA 完全支持它。

你假设的If Range.CommentThreaded Is Nothing作品。

这里有一些代码可以玩:

Private Sub ExcelsNewCommentThreaded()
    Dim AllCommentsThreaded As Excel.CommentsThreaded
    Dim OneCommentThreaded As Excel.CommentThreaded
    Dim AllReplies As Excel.CommentsThreaded
    Dim OneReply As Excel.CommentThreaded
    Dim r As Range

    Set AllCommentsThreaded = ActiveSheet.CommentsThreaded

    ' loop over all threaded comments of a worksheet and get their info
    For Each OneCommentThreaded In AllCommentsThreaded
        With OneCommentThreaded
            Debug.Print .Author.Name, .Date, .Text
            For Each OneReply In .Replies
                With OneReply
                    Debug.Print .Author.Name, .Date, OneReply.Text
                End With
            Next OneReply
        End With
    Next OneCommentThreaded

    Set r = Selection.Cells(1)

    ' check if the selected cell already contains a threaded comment
    If r.CommentThreaded Is Nothing Then
        r.AddCommentThreaded ("my new comment")
    End If

    With r.CommentThreaded
        ' get text of comment
        Debug.Print .Text

        ' add some replies
        .AddReply ("my reply 1")
        .AddReply ("my reply 2")

        ' change text of comment
        Debug.Print .Text(Text:="text of comment changed")
        Debug.Print .Text

        ' change text of a reply
        .Replies(1).Text Text:="text of reply 1 changed"
        Debug.Print .Replies(1).Text

        ' delete second reply
        .Replies(2).Delete

        ' delete whole comment including its replies
        .Delete
    End With
End Sub

推荐阅读