如何通过Excel VBA仅统计已解决的批注数量
Excel VBA 统计已解决与未解决批注的方法
首先明确:
Visible属性无法判断批注是否已解决。该属性仅控制批注是否在工作表中显示,和批注的"已解决/未解决"状态完全无关。要统计已解决批注,需使用
Comment对象的Status属性(适用于Excel 2019及以后版本、Microsoft 365):xlCommentStatusResolved:代表批注已标记为解决xlCommentStatusUnresolved:代表批注未解决
以下是实现统计的VBA代码示例:
Sub CalculateCommentStatus() Dim targetSheet As Worksheet Dim totalCount As Long Dim resolvedCount As Long Dim singleComment As Comment ' 指定要统计的工作表,这里取第一个工作表 Set targetSheet = ThisWorkbook.Worksheets(1) totalCount = targetSheet.Comments.Count resolvedCount = 0 ' 遍历所有批注统计已解决数量 For Each singleComment In targetSheet.Comments If singleComment.Status = xlCommentStatusResolved Then resolvedCount = resolvedCount + 1 End If Next singleComment ' 在立即窗口输出统计结果 Debug.Print "总批注数量: " & totalCount Debug.Print "已解决批注数量: " & resolvedCount Debug.Print "未解决批注数量: " & totalCount - resolvedCount If totalCount > 0 Then Debug.Print "已解决批注占比: " & Round((resolvedCount / totalCount) * 100, 2) & "%" End If End Sub
- 注意事项:如果使用的是Excel 2016及更早版本,Excel本身没有"标记批注为已解决"的功能,自然也不存在对应的属性,这种情况下只能通过手动给批注添加标识(比如在批注内容开头加【已解决】)来实现统计。
内容的提问来源于stack exchange,提问作者Jan_B
相关产品推荐
相关产品推荐

