【问题标题】:Show comments for each sheet of a workbook in a MsgBox在 MsgBox 中显示工作簿每张表的注释
【发布时间】:2017-04-17 17:10:23
【问题描述】:

我正在尝试在MsgBox 中显示Activeworkbook 中每个工作表的所有评论文本(对于每个评论)。

我的代码没有抛出错误,所以我知道我很接近了。

Sub ShowAllWorkbookcomments()

On Error Resume Next

Dim ws As Worksheet
Dim rng As Range
Dim cell As Variant
Dim cmt As String
Dim commentcount As Integer

Set ws = ActiveWorkbook.Worksheets(1)
Set rng = ActiveSheet.Cells.SpecialCells(xlCellTypeComments)
commentcount = rng.Count
'cmt = ws.rng.Comment.Text

Dim varComment As String
Dim c As Comment

For Each ws In ActiveWorkbook.Worksheets

    Select Case commentcount
        Case 0
            MsgBox "No Comment", vbExclamation
            Resume Next
        Case Is > 0            
            For Each cell In rng
                varComment = c.Text
                MsgBox varComment, vbInformation
            Next cell
    End Select

    Set rng = Nothing            
Next ws    

End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您已经很接近了,只需要在For Each ws In ActiveWorkbook.Worksheets 循环中获取Set rng = ActiveSheet.Cells.SpecialCells(xlCellTypeComments)

    另外,添加了另一种方法来捕获工作表没有 cmets 的可能性,并删除了不必要的 Select Case

    试试下面的代码:

    Option Explicit
    
    Sub ShowAllWorkbookcomments()
    
    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim cmt As String
    Dim varComment As String
    
    For Each ws In ActiveWorkbook.Worksheets
        On Error Resume Next 
        Set rng = ws.Cells.SpecialCells(xlCellTypeComments)
        On Error GoTo 0
    
        If Not rng Is Nothing Then '<-- current worksheet has comments
            For Each cell In rng.Cells
                'varComment = cell.Comment.text
                varComment = "worksheet " & ws.Name & " comment " & cell.Comment.text ' <-- added the worksheet name as reference 
                MsgBox varComment, vbInformation
            Next cell
        Else '<-- current worksheet has No comments >> rng is Nothing
            'MsgBox "No Comment", vbExclamation
            MsgBox "worksheet " & ws.Name & " has No Comments", vbExclamation ' <-- added the worksheet name as reference 
        End If
        Set rng = Nothing
    Next ws
    
    End Sub
    

    【讨论】:

    • 感谢它的工作!您是否介意简要解释一下为什么单元格会是 Range 而不是变体或对象? ,以及循环末尾的 Set rng = nothing 正在做什么? (假设它为下一个 WS 清除 rng,然后根据新工作表设置)。最后,当您说 rng.cells 中的每个单元格时,您是否需要 .cells 部分(因为我们已经声明单元格的范围)?
    • @MikeMirabelli 为了循环通过Range,您使用作为单个CellRange,这样您就可以访问Range.Value 或在您的情况下使用cell.Comment.text . Set rng = Nothing 就像你提到的,在 Next ws 之前清除。不用加.Cells,用For Each cell In rng就够了。您可以随意标记为“答案”:)
    • 如果合并单元格上有 cmets,上述逻辑将不起作用。请参考以下答案以解决该问题。 [在合并单元格上循环 cmets] (stackoverflow.com/questions/57643865/…)
    • @DeepakV 仔细阅读采购订单,他没有要求合并单元格
    • @ShaiRado 是的,我知道 PO 没有要求合并单元格。但是,当您在工作表中循环遍历 cmets 并且如果注释位于合并单元格上时,上述代码将返回注释文本“n”次,其中“n”是合并单元格的数量。我最近遇到了这个问题,所以我在这里分享了这些知识。
    【解决方案2】:

    如上面的其中一个 cmets 中所述,上述逻辑将导致 MsgBox 为合并区域中的每个单元格显示。以下逻辑将遍历给定工作表中的 cmets,它也适用于合并的行/列场景。

    For Each CommentedSheets In ActiveWorkbook.Worksheets
        If CommentedSheets.Comments.Count = 0 Then
            MsgBox "worksheet " & CommentedSheets.Name & " has No Comments", vbExclamation
        Else
            For Each Individual_Comment In CommentedSheets.Comments
                varComment = "worksheet " & CommentedSheets.Name & " comment " & Individual_Comment.text
                MsgBox varComment, vbInformation
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-15
      • 2019-05-19
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      相关资源
      最近更新 更多