宏计算工作薄中的注释数量

From 清冽之泉
Jump to navigation Jump to search

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

 1Sub CountCommentsInAllSheets()
 2    Dim wb As Workbook
 3    Dim ws As Worksheet
 4    Dim count As Integer
 5    Dim totalComments As Integer
 6    
 7    Set wb = ThisWorkbook
 8    count = 0
 9    totalComments = 0
10    
11    ' 遍历工作簿中的所有工作表
12    For Each ws In wb.Worksheets
13        ' 遍历当前工作表的每个单元格
14        For Each cell In ws.UsedRange
15            ' 检查单元格是否有注释
16            If Not cell.Comment Is Nothing Then
17                count = count + 1
18            End If
19        Next cell
20        ' 将当前工作表的注释数加到总数
21        totalComments = totalComments + count
22        ' 重置计数器以计算下一个工作表的注释数
23        count = 0
24    Next ws
25    
26    ' 显示包含注释的单元格总数
27    MsgBox "Total number of comments in the workbook: " & totalComments
28End Sub