简体   繁体   English

vba excel - 对于范围内的每个单元格 - 阅读评论

[英]vba excel - For Each cell In range - read comments

there.那里。 I'm trying to retrieve comments from a pre-defined name range of cells, but I keep getting the error message for the instruction: Set c = Range("myRange").Comment.Value我正在尝试从预定义的单元格名称范围中检索注释,但我不断收到指令的错误消息: Set c = Range("myRange").Comment.Value

Only the active workbook is open, range is there, etc. In the meantime, a value has been assigned to c, that corresponds to very first cell of myRange.只有活动的工作簿是打开的,范围是存在的,等等。与此同时,已经为 c 分配了一个值,它对应于 myRange 的第一个单元格。

Any ideas?有任何想法吗? thanks a lot.多谢。

错误信息

返回给 c 的值

Sub Test_findComments_in_range()
Dim varComment As String
Dim c As Variant

With ActiveSheet
    For Each c In .Range("myRange")
    
    Set c = Range("myRange").Comment.Value
        
        If IsEmpty(c) Then
        
        MsgBox "No comment found! "
            
         GoTo jumpCycle3

        Else
            varComment = c.Value
            MsgBox varComment, vbInformation
            
            
        End If
    
    

jumpCycle3:    Next

End With

End Sub

There is no need to go through all the cells in the range and check each one for a comment, you can use the Range.SpecialCells method with the parameter xlCellTypeComments无需 go 遍历范围内的所有单元格并检查每个单元格是否有注释,您可以使用带参数xlCellTypeCommentsRange.SpecialCells 方法

Sub ListComments()
    Dim rng As Range
    
    Set rng = ActiveSheet.Range("myRange")
    On Error GoTo out ' handle the error that occurs if there is no cell with comments
    Set rng = rng.SpecialCells(xlCellTypeComments)
    On Error GoTo 0
    
    For Each cl In rng ' iterate over all cells in rng - each of them contains a comment
        MsgBox cl.Address & " : " & cl.Comment.Text, vbInformation
    Next
    Exit Sub

out:        MsgBox "No comments in the area"
End Sub

Find Comments in a Named Range在命名范围内查找注释

Option Explicit

Sub FindCommentsInNamedRange()

    ' Reference the workbook ('wb').
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Reference the named range ('rg').
    Dim rg As Range: Set rg = wb.Names("myRange").RefersToRange

    ' To reference the range's worksheet (not necessary here), you could use:
    'Dim ws As Worksheet: Set ws = rg.Worksheet
    
    Dim cell As Range
    Dim cm As Comment
    Dim cText As String
    
    ' Loop through the cells of the range (note 'rg.Cells')...
    For Each cell In rg.Cells
        ' Attempt to reference the cell's comment ('cm').
        Set cm = cell.Comment
        ' Validate the comment.
        If cm Is Nothing Then
            MsgBox "No comment found!", vbExclamation, _
                "Comment in Cell '" & cell.Address(0, 0) & "'"
        Else
            ' Write the comment's text to a variable ('cText').
            cText = cm.Text
            MsgBox cText, vbInformation, _
                "Comment in Cell '" & cell.Address(0, 0) & "'"
        End If
    Next cell

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM