VBA - 在选择评论单元格时获取第一个单元格

时间:2017-02-15 14:40:08

标签: excel excel-vba vba

我有几张表,我想阅读每张表的所有评论。 我设法得到了评论,但我能得到的是评论单元格的同一行和列的第一个单元格。(附图)

红色 - 评论过的细胞。

绿色 - 所需的单元格值。

enter image description here

    Sub ShowCommentsAllSheets()
    'Update 20140508
    Dim commrange As Range
    Dim rng As Range
    Dim ws As Worksheet
    Dim newWs As Worksheet
    Set newWs = Application.Worksheets("CRs")
    newWs.Range("A1").Resize(1, 4).Value = Array("Sheet", "A", "Value", "Comment")
    Application.ScreenUpdating = False
    On Error Resume Next
    For Each ws In Application.ActiveWorkbook.Worksheets
        Set commrange = ws.Cells.SpecialCells(xlCellTypeComments)
        If Not commrange Is Nothing Then
            i = newWs.Cells(Rows.Count, 1).End(xlUp).Row
            For Each rng In commrange
                i = i + 1
                newWs.Cells(i, 1).Resize(1, 4).Value = Array(ws.Name, rng.Address, rng.Value, rng.Comment.Text)
            Next
        End If
        Set commrange = Nothing
    Next
    newWs.Cells.WrapText = False
    Application.ScreenUpdating = True
    End Sub

1 个答案:

答案 0 :(得分:1)

或许只是改变这一行?

newWs.Cells(i, 1).Resize(1, 5).Value = Array(ws.Name, rng.Address, rng.Value, rng.Comment.Text, ws.Cells(rng.Row, 2))