是否可以根据使用计数器的条件在注释中添加多行?

时间:2016-06-12 08:37:28

标签: vba excel-vba excel

我有这段代码,

For m = 1 To no_of_ind
        For x = 2 To mx + 2
            If ws.Cells(x + 1, n).Comment.Text = Worksheets("Intermediate").Cells(m, n) Then
               ws.Cells(x + mx + 4, n).AddComment ws.Cells(x + 1, n).Value
            Else
            'do nothing
            End If
        Next x
Next m

每当满足if条件时,我都会向特定单元格添加注释。如果条件不止一次满足,我想在评论中添加尽可能多的行和适当的文本。

2 个答案:

答案 0 :(得分:1)

即使我需要知道如何做这样的事情,我也会记录一个宏

Range("A1").addComment
Range("A1").Comment.Visible = False
Range("A1").Comment.Text Text:="Waldo:" & Chr(10) & "Sales are down %20 percent!!!"
Selection.ShapeRange.ScaleWidth 1.29, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 1.78, msoFalse, msoScaleFromTopLeft
Range("F4").Select

下一个重构:

With Range("A1").addComment
    .Comment.Visible = False
    .Comment.Text Text:="Waldo:" & Chr(10) & "Sales are down %20 percent!!!"
End With

一次又一次地重构......

Function addComment(TargetCell As Range, txt As String, isVisible As Boolean)

    If TargetCell.Comment Is Nothing Then TargetCell.addComment

    With TargetCell.Comment
        .Comment.Visible = isVisible
        .Comment.Text Text:= txt
    End With

End Function

以下是一些方便的字符代码

  • Tab:char(9)
  • 换行:char(10)
  • 双引号:char(34)

答案 1 :(得分:1)

试试这个:

For m = 1 To no_of_ind
    For x = 2 To mx + 2
        If Not(ws.Cells(x + 1, n).Comment) Is Nothing then
            CurrentComment = ws.Cells(x + 1, n).Comment.Text
        End If
        If ws.Cells(x + 1, n).Comment.Text = Worksheets("Intermediate").Cells(m, n) Then
            ws.Cells(x + mx + 4, n).Comment.Delete
            ws.Cells(x + mx + 4, n).AddComment CurrentComment & vbCrLf & ws.Cells(x + 1, n).Value
        Else
            'do nothing
        End If
    Next x
Next m

您需要删除当前评论以添加其他评论,至少我无法在不删除当前评论的情况下使其发挥作用。代码将在不同的行中给出两个注释。