Excel批注提取:当效率遇上简洁

在月末的最后一天需要查询“4月1日"..."4月30日"30个工作表中的批注,如下DeepSeek的代码像一艘经过流体力学设计的快艇。

Sub ExtractComments()
Dim commentCount As Integer
Dim comments() As Variant
Dim day As Integer
Dim outputWs As Worksheet
Dim targetWs As Worksheet
Dim wsName As String

On Error Resume Next
Set outputWs = Worksheets("批注列表")
On Error GoTo 0
If outputWs Is Nothing Then
  Set outputWs = Worksheets.Add(after:=Worksheets(Worksheets.Count))
  outputWs.Name = "批注列表"
End If

outputWs.Cells.Clear
outputWs.Range("a1:c1") = Array("表格名", "单元格地址", "批注内容")

commentCount = 0
ReDim comments(1 To 3, 1 To 1)

For day = 1 To 30
  Dim cell As Range
  wsName = "4月" & day & "日"
  Set targetWs = Worksheets(wsName)
  If Not targetWs Is Nothing Then
    For Each cell In targetWs.UsedRange
      If Not cell.Comment Is Nothing Then
        commentCount = commentCount + 1
        ReDim Preserve comments(1 To 3, 1 To commentCount)
        comments(1, commentCount) = wsName
        comments(2, commentCount) = cell.Address
        comments(3, commentCount) = cell.Comment.Text
      End If
    Next cell
  End If
  
Next day

If commentCount > 0 Then
  outputWs.Range("a2").Resize(commentCount, 3).Value = WorksheetFunction.Transpose(comments)
End If

End Sub

通义和Kimi的代码方案则像瑞士军刀般轻巧。它们直接遍历每个单元格的简单逻辑,在只有零星批注的周报中游刃有余。这种"够用就好"的特性,在日常少量数据处理时反而成为优势。

Sub ListComments()
Dim ws As Worksheet
Dim resultSheet As Worksheet
Dim rowIndex As Integer
Dim sheetName As String
Dim cell As Range

On Error Resume Next
Set resultSheet = Worksheets("批注查询")
On Error GoTo 0
If resultSheet Is Nothing Then
  Set resultSheet = Worksheets.Add(after:=Sheets(Sheets.Count))
  resultSheet.Name = "批注查询"
End If
resultSheet.Cells.Clear
rowIndex = 1
resultSheet.Cells(rowIndex, 1).Value = "表格名称"
resultSheet.Cells(rowIndex, 2).Value = "单元格地址"
resultSheet.Cells(rowIndex, 3).Value = "批注内容"

For Each ws In Worksheets
  If Left(ws.Name, 2) = "4月" Then
    sheetName = ws.Name
    For Each cell In ws.UsedRange
      If Not cell.Comment Is Nothing Then
        rowIndex = rowIndex + 1
        resultSheet.Cells(rowIndex, 1).Value = sheetName
        resultSheet.Cells(rowIndex, 2).Value = cell.Address
        resultSheet.Cells(rowIndex, 3).Value = cell.Comment.Text
      End If
    Next cell
 End If
Next ws
  
End Sub

个人感觉都不错,当面对季度报告等海量批注时,应启动DeepSeek的"涡轮增压"模式:通过数组缓存、批量操作等机制,其性能优势可扩大至普通代码的5-7倍。

日常零星批注管理则适合通义和Kimi轻量方案:无需记忆复杂参数,即改即用的特性让临时需求处理更从容。
这个案例揭示了一个永恒的编程真理:没有绝对的最优解,只有针对场景的适应性选择。就像财务人员总会准备两套账本系统,我们处理数据时也需要在效率与简洁之间保持动态平衡——这正是职场生存的智慧所在。

posted @ 2025-04-07 22:37  孤独的小苗  阅读(19)  评论(0)    收藏  举报