写长NoteText

时间:2018-01-31 14:22:34

标签: excel vba excel-vba

我正在尝试在Excel中使用VBA来撰写评论。如果评论足够短,它可以正常工作,但如果评论太长,它就不再写评论了。在下面的示例中,如果我在最后一个省略号之后剪切注释,它将起作用。这个问题有什么解决方案吗?

Sub longComment()
Cells(1, 3).NoteText Text:="Hello, I am a very long comment. Why can't I be written as a comment? It seems there is something very strange happening! Does anyone know what's wrong with me? How can I avoid this problem? See what happens when I add another line ... and another one ... and one more still!"
End Sub

3 个答案:

答案 0 :(得分:3)

这是我设法得到的:

Sub allWeNeedIsLongComment()

    Dim commentToBeAdded    As String
    Dim commentSigns        As Long
    Dim cnt                 As Long
    Dim addToFixTheLoop     As Long

    commentToBeAdded = "Here is FatBox Slim...." & vbCrLf & _
                       "1.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "2.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "3.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "4.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "5.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "6.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "7.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "8.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "9.  Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "10. Push the tempo! Push the tempo!Push the tempo!" & vbCrLf & _
                       "Oh push the tempo!"

    commentSigns = Len(commentToBeAdded)
    If Not Range("C3").Comment Is Nothing Then Range("C3").Comment.Delete
    addToFixTheLoop = commentSigns Mod 255 + 1

    For cnt = 1 To (commentSigns + addToFixTheLoop) Step 255
        If cnt = 1 Then
            Range("C3").NoteText Text:=Mid(commentToBeAdded, cnt, 255)
        Else
            Range("C3").NoteText Text:=Mid(commentToBeAdded, cnt, 255), Start:=cnt

        End If
    Next cnt

End Sub

这就是它的样子:

enter image description here

或者你可以简单地使用.Comment而不关心循环等。

答案 1 :(得分:2)

要添加包含超过255个字符的注释,请使用此方法一次指定前255个字符,然后再次使用它来附加注释的其余部分(一次不超过255个字符)。

NoteText Documentation

答案 2 :(得分:2)

您可以直接使用其NoteText方法没有令人讨厌的255个字符限制的注释对象,而不是使用Text方法:

Sub test()
    If Range("A1").Comment Is Nothing Then Range("A1").AddComment
    Range("A1").Comment.Text String(10000, "*")
End Sub