在Excel中逐行将便笺转移到自己的位置



我有一个非常大的excel文件,在不同的单元格上有数百个注释。我想提取每行的注释内容并将它们放在自己的列中。例如,如果我在第 1 行中有 3 个注释,则这些注释中的文本将放在 U1 中。如果 4 条评论在第 2 行,那么这 4 条评论将在 U2 中,依此类推。到目前为止,我正在使用 VBA 来执行此操作,但无法让它们按行分隔。

Sub CopyCommentsToCol()
Dim i As Integer
i = 2
Dim Rng As Range
Dim cell As Range
Dim row As Range
Dim commrange As Range
Dim curwks As Worksheet
Set Rng = Range("A2:A5") 'Test Range for now
Set curwks = ActiveSheet
On Error Resume Next
  Set commrange = curwks.Cells _
      .SpecialCells(xlCellTypeComments)
On Error GoTo 0

On Error Resume Next
If Err.Number <> 0 Then
  Err.Clear
End If
For Each row In Rng.Rows
    For Each cell In commrange 'Application.ActiveCell.Comment
        If cell.Comment <> Empty Then
            Range("$U$" & i) = Range("$U$" & i).Text & cell.Comment.Text 
        End If
    Next cell
    i = i + 1
Next row
End Sub

此 vba 代码当前将所有音符放在我指定的测试范围内。不仅仅是自己行中的笔记。我在这里理解我的错误,内部 for 循环正在遍历整个工作表。我只是不知道如何解决这个问题。

编辑

For Each row In Rng.Rows
    Set commrange = row.SpecialCells(xlCellTypeComments)
    For Each cell In commrange
        If cell.Comment <> Empty Then
            Range("$U$" & i) = Range("$U$" & i).Text & cell.Comment.Text
        End If
    Next cell
    i = i + 1
Next row
您可以使用

Rows集合。类似的东西

For Each row In yourRange.Rows
    'collect comments
Next row

更新:

由于第一个想法不起作用,因此您可以选中cell.Row并在单元格中添加文本时使用它。

Sub CopyCommentsToCol()
Dim Rng As Range
Dim cell As Range
Dim row As Range
Dim commrange As Range
Dim curwks As Worksheet
Set Rng = Range("A2:A5") 'Test Range for now
Set curwks = ActiveSheet
On Error Resume Next
  Set commrange = curwks.Cells _
      .SpecialCells(xlCellTypeComments)
On Error GoTo 0

On Error Resume Next
If Err.Number <> 0 Then
  Err.Clear
End If
For Each cell In commrange 'Application.ActiveCell.Comment
    If cell.Comment <> Empty Then
        Range("$U$" & cell.Row) = Range("$U$" & cell.Row).Text & cell.Comment.Text 
    End If
Next cell
End Sub

最新更新