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

如何调整VBA代码实现按PO编号归集行数据至Collection

VBA代码调整方案(实现PO分组归集SKU与数量)

一、调整类模块(命名为clsPOData)

更新类模块用于存储单个PO的全量物料信息:

Public PO As String
Public SKU1 As String
Public SKU2 As String
Public SKU3 As String
Public SKU4 As String
Public SKU5 As String
Public Quantity1 As Integer
Public Quantity2 As Integer
Public Quantity3 As Integer
Public Quantity4 As Integer
Public Quantity5 As Integer
Private itemCount As Integer ' 内部计数器,跟踪当前PO下的物料数量

' 初始化计数器
Private Sub Class_Initialize()
    itemCount = 0
End Sub

' 添加SKU和数量的方法
Public Sub AddItem(sku As String, qty As Integer)
    itemCount = itemCount + 1
    If itemCount > 5 Then Exit Sub ' 超过5个物料则忽略
    ' 动态赋值对应SKU和Quantity属性
    Me("SKU" & itemCount) = sku
    Me("Quantity" & itemCount) = qty
End Sub

二、标准模块核心代码实现

以下代码完成数据遍历、PO信息归集及结果输出:

Sub GroupPOData()
    Dim poColl As New Collection
    Dim poData As clsPOData
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim currentPO As String
    Dim currentSKU As String
    Dim currentQty As Integer
    Dim exists As Boolean
    Dim j As Long
    
    Set ws = ThisWorkbook.Worksheets("Sheet1") ' 替换为你的数据工作表名
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' 假设PO编号在A列
    
    ' 遍历数据行(从第2行开始,默认第1行为表头)
    For i = 2 To lastRow
        currentPO = ws.Cells(i, "A").Value
        currentSKU = ws.Cells(i, "B").Value ' 假设Vendor Item#在B列
        currentQty = ws.Cells(i, "C").Value ' 假设Qty在C列
        
        ' 检查当前PO是否已在集合中
        exists = False
        For j = 1 To poColl.Count
            If poColl(j).PO = currentPO Then
                exists = True
                ' 调用类方法追加新物料
                poColl(j).AddItem currentSKU, currentQty
                Exit For
            End If
        Next j
        
        ' 若PO不存在,新建类实例并加入集合
        If Not exists Then
            Set poData = New clsPOData
            poData.PO = currentPO
            poData.AddItem currentSKU, currentQty
            poColl.Add poData, Key:=currentPO ' 用PO编号作为集合唯一Key
        End If
    Next i
    
    ' 输出归集结果(可按需修改为写入工作表或其他格式)
    Debug.Print "归集结果:"
    For Each poData In poColl
        Debug.Print "Key: " & poData.PO
        For j = 1 To 5
            If poData("SKU" & j) <> "" Then
                Debug.Print "SKU" & j & ": " & poData("SKU" & j) & ", Quantity" & j & ": " & poData("Quantity" & j)
            End If
        Next j
        Debug.Print "------------------------"
    Next poData
End Sub

三、关键逻辑说明

  • 类模块封装:通过AddItem方法自动管理SKU和Quantity的索引赋值,同时限制单PO最多5个物料,避免手动判断的冗余代码。
  • 集合唯一性控制:使用PO编号作为Collection的Key,确保每个PO仅存一份实例;遍历集合检查PO存在性,存在则追加物料,不存在则新建实例。
  • 输出灵活性:当前通过Debug.Print输出到立即窗口,可根据需求修改为写入指定工作表区域或生成报表。

四、注意事项

  1. 请根据实际数据列位置,调整代码中ws.Cells(i, "A")、ws.Cells(i, "B")等列标识。
  2. 确保类模块命名为clsPOData,否则代码会触发对象未定义错误。
  3. 若需取消单PO5个物料的限制,可删除AddItem方法中的If itemCount > 5 Then Exit Sub语句。

内容的提问来源于stack exchange,提问作者Eli Greenberg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:37:23