【问题标题】:Commenting on cells评论单元格
【发布时间】:2022-01-28 07:22:26
【问题描述】:

所以我有一些代码可以根据旁边的信息对单元格进行 cmets,它几乎可以完美地工作。但是,我遇到了一个问题,它正在处理整个工作簿中的所有工作表,而不是像它应该的那样只处理工作表 3。我仅在 VBA 项目中的工作表 3 上设置了此代码。所以我的问题是我如何才能只在一张纸上完成这项工作? 这是我的代码

Private Sub Worksheet_Calculate()

ActiveSheet.UsedRange.ClearComments

Dim targetRng As Range, commentSrcRng As Range
Dim strPrefix As String 'string Prefix

Set targetRng = Application.Range("D16,D18,D20,D22,D24,D26,D28,D30,D32,D34,D36,D38")
Set commentSrcRng = targetRng.Offset(0, 1)

Dim cel As Range
Dim i As Long
i = 1
    For Each cel In targetRng
        If cel <> "" Then
            cel.AddComment
                cel.Comment.Visible = False
                cel.Comment.Shape.TextFrame.AutoSize = True
                cel.Comment.Text strPrefix & commentSrcRng.Cells(i)
        End If
            i = i + 2
    Next

【问题讨论】:

    标签: excel vba comments


    【解决方案1】:

    但是,我遇到了一个问题,它正在处理整个工作簿中的所有工作表

    那是因为您已将其编码为在 ActiveSheet 上工作

    要引用事件编码所在的工作表,请使用Me

    Private Sub Worksheet_Calculate()
        Me.UsedRange.ClearComments
    
        Dim targetRng As Range, commentSrcRng As Range
        Dim strPrefix As String 'string Prefix
    
        Set targetRng = Me.Range("D16,D18,D20,D22,D24,D26,D28,D30,D32,D34,D36,D38")
        Set commentSrcRng = targetRng.Offset(0, 1)
    
        Dim cel As Range
        Dim i As Long
        i = 1
        For Each cel In targetRng
            If cel <> "" Then
                cel.AddComment
                cel.Comment.Visible = False          cel.Comment.Shape.TextFrame.AutoSize = True
                cel.Comment.Text strPrefix & commentSrcRng.Cells(i)
            End If
            i = i + 2
        Next
    End Sub
    

    【讨论】:

    • 非常感谢!那解决了它。供将来参考,我。特指我写代码的工作表正确吗?
    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多