You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求VBA宏代码:计算Level 0任务各阶段Completed值占比

VBA宏:统计Level 0任务下属Level 1任务的完成占比

以下是满足需求的VBA代码,它会遍历Excel表格中的Level 0任务,统计其下属所有Level 1任务在Developed(D列)、Tested(E列)、ReadyForRelease(F列)中标记为“Completed”的占比,并将结果写入对应Level 0任务的D、E、F、G列(G列默认设置为三项完成率的平均值,可按需调整)。

Sub CalculateLevel0CompletionRatio()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim level0Row As Long, level1Row As Long
    Dim devCount As Integer, testCount As Integer, rfrCount As Integer
    Dim totalLevel1 As Integer
    
    ' 替换为你的目标工作表名称
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' 从第2行开始遍历(假设第1行是表头)
    level0Row = 2
    Do While level0Row <= lastRow
        ' 判定当前行是否为Level 0任务(假设Level标识在B列,可按需修改列号)
        If ws.Cells(level0Row, "B").Value = "Level 0" Then
            devCount = 0
            testCount = 0
            rfrCount = 0
            totalLevel1 = 0
            
            ' 统计当前Level 0下属的连续Level 1任务
            level1Row = level0Row + 1
            Do While level1Row <= lastRow And ws.Cells(level1Row, "B").Value = "Level 1"
                totalLevel1 = totalLevel1 + 1
                
                ' 统计各列的Completed数量
                If ws.Cells(level1Row, "D").Value = "Completed" Then devCount = devCount + 1
                If ws.Cells(level1Row, "E").Value = "Completed" Then testCount = testCount + 1
                If ws.Cells(level1Row, "F").Value = "Completed" Then rfrCount = rfrCount + 1
                
                level1Row = level1Row + 1
            Loop
            
            ' 计算占比并写入单元格(避免除以0错误)
            If totalLevel1 > 0 Then
                ws.Cells(level0Row, "D").Value = devCount / totalLevel1
                ws.Cells(level0Row, "E").Value = testCount / totalLevel1
                ws.Cells(level0Row, "F").Value = rfrCount / totalLevel1
                ' G列默认取三项完成率的平均值,可修改为其他逻辑
                ws.Cells(level0Row, "G").Value = (devCount + testCount + rfrCount) / (totalLevel1 * 3)
            Else
                ' 无下属Level 1时填充0
                ws.Range(ws.Cells(level0Row, "D"), ws.Cells(level0Row, "G")).Value = 0
            End If
            
            ' 将结果单元格格式化为百分比(可选,可删除)
            ws.Range(ws.Cells(level0Row, "D"), ws.Cells(level0Row, "G")).NumberFormat = "0.00%"
        End If
        
        level0Row = level0Row + 1
    Loop
    
    MsgBox "统计完成!", vbInformation
End Sub

关键调整说明:

  • 工作表与列匹配:代码中默认工作表为Sheet1、Level标识在B列,需根据你的实际表格结构修改对应的参数。
  • 层级判定逻辑:当前代码仅统计Level 0之后连续的Level 1任务,若你的层级结构是非连续排列,需调整Level 1的判定条件。
  • G列逻辑:当前设置为三项完成率的平均值,你可根据需求修改为取最小值、最大值或其他汇总规则。
  • 错误处理:针对无下属Level 1的情况,自动填充0避免除以0的运行错误。

使用步骤:

  1. 打开目标Excel文件,按下Alt + F11打开VBA编辑器。
  2. 右键点击工作簿名称,选择「插入」→「模块」。
  3. 将上述代码粘贴到模块中,调整工作表和列的参数以匹配你的表格。
  4. 按下F5运行宏,或通过Excel「开发工具」选项卡执行该宏。

内容的提问来源于stack exchange,提问作者vijayaraghavan Devaraj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 16:05:10