如果其他列中的单元格包含值,则向单元格添加注释



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

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

表格看起来像这样:表格

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

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列中的内容检查工作。有人知道如何让这一点发挥作用吗?

尝试以下内容。还要检查如何避免选择以及为什么使用长而不是整数

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

相关内容

最新更新