vba - 在 Excel 中将注释逐行传输到自己的列

标签 vba excel

我有一个非常大的 Excel 文件,其中不同单元格上有数百个注释。我想提取每一行的注释内容并将它们放在自己的列中。例如,如果我在第 1 行中有 3 个注释,那么这些注释中的文本将放入 U1 中。如果第 2 行有 4 条评论,那么这 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

关于vba - 在 Excel 中将注释逐行传输到自己的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44700687/

相关文章:

vba - 如何选择正在运行的 Word 实例并在其中打开文档

java - 如何将excel表复制到同一个工作簿?

arrays - 修改现有函数以处理不同维度/结构的数组

VBA 输入框去除小数逗号,将 double 转换为字符串

Excel VBA : vlookup to find row of date in date range table

excel - VBA 网页抓取脚本返回下标超出范围

macos - 为什么在 Excel for Mac 中使用 VBA 的输入框不显示提示文本?

excel - 检测单元格中的粗体文本并在这些文本前添加 ";"

vba - excel vba中最大值的地址

Excel VBA 运行时错误 1004 仅适用于以 'c' 开头的名称