我的办公室刚刚升级到新版本的 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
Run Code Online (Sandbox Code Playgroud)
截至 2019 年 5 月 15 日,MicrosoftCommentThreaded描述了新对象。
在我的 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
Run Code Online (Sandbox Code Playgroud)