【问题标题】:Add comment to cell if cell in other column contains value如果其他列中的单元格包含值,则向单元格添加注释
【发布时间】:2022-01-27 04:15:27
【问题描述】:

我有一个代码,它结合了 C:F 列中所有单元格的单元格内容,并将其放入 B 列的注释中 - 每行。 我现在只需要将其应用于在各自的 A 列中有内容的行。

单元格 A2 里面有东西,所以把 C2:F2 的内容放到 B2 的注释中。 单元格 A3 中没有任何内容,因此不要向该单元格添加注释。 单元格A4又有东西了,把C4:F4的内容放到B4的注释里面。

表格如下所示:Table

到目前为止,我的代码如下所示:

Sub Test()
    Dim LRow As Integer
    
    With ActiveSheet
        For LRow = 2 To Range("A" & Rows.Count).End(xlUp).Row
        Range(Cells(LRow, 3), Cells(LRow, 6)).Select
        
        Dim c As Range, s As String

        With Cells(LRow, 2)
            .ClearComments
            For Each c In Selection
            'If c.Offset(0, -2) <> "" Then
                'On Error Resume Next
                If c <> "" Then s = IIf(s = "", c, s & Chr(10) & c)
                Next c
                .AddCommentThreaded "Test:" & Chr(10) & s
        End With
        s = ""
        Next LRow
    End With
End Sub

现在的问题是我无法让 A 列中的内容检查正常工作。有人对如何使该位起作用有任何提示吗?

【问题讨论】:

  • If Range("A" &amp; lRow).Value &lt;&gt; "" 也许?

标签: excel vba


【解决方案1】:

试试下面的方法。还有checkout how to avoid selectwhy use long instead of integer

Sub Test()
    Dim LRow As Long, aCell As Range, ws As Worksheet
    Set ws = ActiveSheet
    
    With ws
    
        For LRow = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            If .Cells(LRow, 1).Value <> "" Then
                Dim theComment As String
                theComment = ""
                
                For Each aCell In Intersect(Range("C:F"), .Rows(LRow)).Cells
                    theComment = theComment & aCell.Value
                Next aCell
    
            With .Cells(LRow, 2)
                .ClearComments
                    .AddCommentThreaded "Test:" & Chr(10) & theComment
            End With
            End If
            
        Next LRow
    End With
End Sub

【讨论】:

  • 非常感谢你,好人。非常我正在寻找的东西,只是无法获得第 1 列检查的位置 - 另外,请提供一般提示,干杯。
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多