vba代码显示WB中每个工作表的所有注释(msgbox)

我正在尝试编写一个子程序,以显示MsgBox Activeworkbook中每个工作表的所有注释文本(对于每个注释)。 我的代码是缺less一些事情,使其工作,但现在它不是抛出一个错误,所以我知道我很接近。

任何人都可以帮我吗?

 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 

你很近,只需要For Each ws In ActiveWorkbook.Worksheets循环中的For Each ws In ActiveWorkbook.Worksheets里面findSet rng = ActiveSheet.Cells.SpecialCells(xlCellTypeComments)

另外,还增加了另一种方法来捕捉没有评论的工作表的可能性,并删除了不必要的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