vba - AddComment 导致应用程序定义或对象定义的错误

标签 vba excel comments

我在尝试使用 .AddComment 时不断收到错误消息方法,我尝试了各种表达方式,但没有运气。目前我的代码读取

Cells(cellRow, CellCol).Select
With Selection
    .Interior.color = vbRed
    .AddComment
        With .Comment
         .Text Text:helpfulComment
         .Shape.ScaleWidth 5.6, msoFalse, msoScaleFromTopLeft
         .Shape.ScaleHeight 1, msoFalse, msoScaleFromTopLeft
        End with
End With

错误消息读取,

Run-time Error '1004' - Application-defined or object-defined error



中断 .Addcomment线

最佳答案

它是因为您不能向已经有评论的范围添加评论

所以你有两种方法

第一种方式 - 检查评论是否已经存在

Sub main()
    With Cells(cellRow, CellCol) 'reference wanted range
        .Interior.Color = vbRed
        If .Comment Is Nothing Then .AddComment ' Add a comment if not already there
        With .Comment ' reference the comment object of the referenced range
            .Text "Text: helpfulComment"
            .Shape.ScaleWidth 5.6, msoFalse, msoScaleFromTopLeft
            .Shape.ScaleHeight 1, msoFalse, msoScaleFromTopLeft
        End With
    End With
End Sub

第二种方式 - 无论如何清除评论(如果范围没有评论,则不会抛出任何错误)并在之后添加它
Sub main()
    With Cells(cellRow, CellCol) 'reference wanted range
        .Interior.Color = vbRed
        .ClearComments ' Clear referenced range comments, if any
        With .AddComment ' add and reference a new comment object of the referenced range
            .Text "Text: helpfulComment"
            .Shape.ScaleWidth 5.6, msoFalse, msoScaleFromTopLeft
            .Shape.ScaleHeight 1, msoFalse, msoScaleFromTopLeft
        End With
    End With
End Sub

关于vba - AddComment 导致应用程序定义或对象定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48747265/

相关文章:

PHP MYSQL - ID AS 类别 SUB_ID 作为注释

平均值的 Excel 宏

vba - 使用 VBA 将标签与 Excel 流程图中的箭头匹配

c++ -//C++ 中的注释与 C 中的/* */

string - 如何使用 Excel 函数获取字符串的最后一个字符?

excel - 使用年份和儒略日期确定已经过去了多少天的公式

Objective-C方法说明(文档注释)

excel - 通过 VBA 发送多封电子邮件

excel - 当数组为空时处理错误 9

excel - VB : Copy a Range to another Excel Application