Excel macro to count comments in a workbook: Difference between revisions

From 清冽之泉
Jump to navigation Jump to search
Created page with "无权要求其改变工作习惯的同事,喜欢在单元格上用注释,要计算他搞了多少条注释,手动数是不切实际的,Kimi.ai 写一段宏解决了此问题。 <syntaxhighlight lang="VBScript" line> Sub CountCommentsInAllSheets() Dim wb As Workbook Dim ws As Worksheet Dim count As Integer Dim totalComments As Integer Set wb = ThisWorkbook count = 0 totalComments = 0 ' 遍历工作簿中的所有工作..."
 
No edit summary
 
Line 31: Line 31:
End Sub
End Sub
</syntaxhighlight>
</syntaxhighlight>
[[Category:MicroSoft Office]]

Latest revision as of 23:44, 6 December 2024

无权要求其改变工作习惯的同事,喜欢在单元格上用注释,要计算他搞了多少条注释,手动数是不切实际的,Kimi.ai 写一段宏解决了此问题。

Sub CountCommentsInAllSheets()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim count As Integer
    Dim totalComments As Integer
    
    Set wb = ThisWorkbook
    count = 0
    totalComments = 0
    
    ' 遍历工作簿中的所有工作表
    For Each ws In wb.Worksheets
        ' 遍历当前工作表的每个单元格
        For Each cell In ws.UsedRange
            ' 检查单元格是否有注释
            If Not cell.Comment Is Nothing Then
                count = count + 1
            End If
        Next cell
        ' 将当前工作表的注释数加到总数
        totalComments = totalComments + count
        ' 重置计数器以计算下一个工作表的注释数
        count = 0
    Next ws
    
    ' 显示包含注释的单元格总数
    MsgBox "Total number of comments in the workbook: " & totalComments
End Sub