' ======================================== ' 模块: modBOMTest ' 用途: 测试和使用BOM数据结构-同步测试2 ' ======================================== Option Explicit Sub TestBOMStructure() ' 初始化BOM管理器 Dim bomMgr As New clsBOMManager ' 加载数据(假设工作表名称) Dim wsConfig As Worksheet Dim wsPlatform As Worksheet Set wsConfig = ThisWorkbook.Worksheets("领料配置") Set wsPlatform = ThisWorkbook.Worksheets("平台配置清单") ' 加载数据 bomMgr.LoadData wsConfig, wsPlatform ' 打印类别树结构到新工作表 ' Dim wsOutput As Worksheet ' On Error Resume Next ' Application.DisplayAlerts = False ' ThisWorkbook.Worksheets("BOM结构").Delete ' Application.DisplayAlerts = True ' On Error GoTo 0 ' ' Set wsOutput = ThisWorkbook.Worksheets.Add ' wsOutput.Name = "BOM结构" 'bomMgr.PrintCategoryTree wsOutput Dim cats As Collection Dim i As Integer Set cats = bomMgr.GetRootCategories() For i = 1 To cats.Count Debug.Print cats(i).categoryName Next i 'MsgBox "BOM数据结构加载完成!" & vbCrLf & _ "请查看 'BOM结构' 工作表", vbInformation End Sub ' 示例: 获取特定类别的物料 Sub GetCategoryMaterials() Dim bomMgr As New clsBOMManager Dim wsConfig As Worksheet, wsPlatform As Worksheet Set wsConfig = ThisWorkbook.Worksheets("领料配置") Set wsPlatform = ThisWorkbook.Worksheets("平台配置清单") bomMgr.LoadData wsConfig, wsPlatform ' 获取"部件"类别的物料(使用父类别) Dim Materials As Collection Set Materials = bomMgr.GetMaterialsForPicking("部件", True) Debug.Print "部件类别物料数量(父类别): " & Materials.Count ' 获取"部件"类别的物料(使用子类别) Set Materials = bomMgr.GetMaterialsForPicking("部件", False) Debug.Print "部件类别物料数量(子类别展开): " & Materials.Count ' 遍历物料 Dim mat As clsMaterialItem For Each mat In Materials Debug.Print mat.code & " - " & mat.Name & _ " | 数量:" & mat.Quantity & _ " | 条件:" & mat.Condition Next mat End Sub ' 示例: 根据产品型号生成领料清单 Sub GeneratePickingList() Dim bomMgr As New clsBOMManager Dim wsConfig As Worksheet, wsPlatform As Worksheet Set wsConfig = ThisWorkbook.Worksheets("领料配置") Set wsPlatform = ThisWorkbook.Worksheets("平台配置清单") bomMgr.LoadData wsConfig, wsPlatform ' 创建领料清单工作表 Dim wsPickList As Worksheet On Error Resume Next Application.DisplayAlerts = False ThisWorkbook.Worksheets("领料清单").Delete Application.DisplayAlerts = True On Error GoTo 0 Set wsPickList = ThisWorkbook.Worksheets.Add wsPickList.Name = "领料清单" ' 写入表头 Dim row As Long row = 1 wsPickList.Cells(row, 1).Value = "代号" wsPickList.Cells(row, 2).Value = "名称" wsPickList.Cells(row, 3).Value = "类别" wsPickList.Cells(row, 4).Value = "数量" wsPickList.Cells(row, 5).Value = "选择条件" wsPickList.Cells(row, 6).Value = "领料方式" row = row + 1 ' 遍历所有根类别 Dim rootCats As Collection Set rootCats = bomMgr.GetRootCategories Dim cat As clsCategory Dim Materials As Collection Dim mat As clsMaterialItem Dim i As Long, j As Long For i = 1 To rootCats.Count Set cat = rootCats(i) ' 默认使用父类别物料 Set Materials = bomMgr.GetMaterialsForPicking(cat.categoryName, True) For j = 1 To Materials.Count Set mat = Materials(j) wsPickList.Cells(row, 1).Value = mat.code wsPickList.Cells(row, 2).Value = mat.Name wsPickList.Cells(row, 3).Value = mat.Category wsPickList.Cells(row, 4).Value = mat.Quantity wsPickList.Cells(row, 5).Value = mat.Condition wsPickList.Cells(row, 6).Value = "父类别" row = row + 1 Next j Next i ' 格式化表格 wsPickList.Range("A1:F1").Font.Bold = True wsPickList.Columns("A:F").AutoFit MsgBox "领料清单生成完成!", vbInformation End Sub ' 示例: 在立即窗口打印领料清单 Sub PrintPickingList() Dim bomMgr As New clsBOMManager Dim wsConfig As Worksheet, wsPlatform As Worksheet Set wsConfig = ThisWorkbook.Worksheets("领料配置") Set wsPlatform = ThisWorkbook.Worksheets("平台配置清单") bomMgr.LoadData wsConfig, wsPlatform ' 打印表头 Debug.Print String(80, "=") Debug.Print "领料清单" Debug.Print String(80, "=") ' 遍历所有根类别 Dim rootCats As Collection Set rootCats = bomMgr.GetRootCategories Dim cat As clsCategory Dim Materials As Collection Dim mat As clsMaterialItem Dim i As Long, j As Long Dim totalCount As Long totalCount = 0 For i = 1 To rootCats.Count Set cat = rootCats(i) ' 打印类别分隔符 Debug.Print "" Debug.Print "【类别: " & cat.categoryName & "】" Debug.Print String(40, "-") ' 默认使用父类别物料 Set Materials = bomMgr.GetMaterialsForPicking(cat.categoryName, True) For j = 1 To Materials.Count Set mat = Materials(j) Debug.Print " " & mat.code & _ String(20 - Len(mat.code), " ") & _ mat.Name & _ " | 数量:" & mat.Quantity & _ " | 条件:" & IIf(mat.Condition = "", "(无)", mat.Condition) totalCount = totalCount + 1 Next j Debug.Print " 小计: " & Materials.Count & " 项" Next i ' 打印汇总 Debug.Print "" Debug.Print String(80, "=") Debug.Print "总计: " & totalCount & " 项物料" Debug.Print String(80, "=") End Sub ' 示例: 查询特定物料信息 Sub QueryMaterialInfo() Dim bomMgr As New clsBOMManager Dim wsConfig As Worksheet, wsPlatform As Worksheet Set wsConfig = ThisWorkbook.Worksheets("领料配置") Set wsPlatform = ThisWorkbook.Worksheets("平台配置清单") bomMgr.LoadData wsConfig, wsPlatform ' 查询特定类别 Dim cat As clsCategory Set cat = bomMgr.GetCategory("部件") If Not cat Is Nothing Then Debug.Print "类别: " & cat.categoryName Debug.Print "父类别: " & cat.ParentCategoryName Debug.Print "物料数: " & cat.Materials.Count Debug.Print "子类别数: " & cat.SubCategories.Count Debug.Print "是否叶子类别: " & cat.IsLeafCategory ' 列出所有物料 Dim mat As clsMaterialItem Dim i As Long For i = 1 To cat.Materials.Count Set mat = cat.Materials(i) Debug.Print " - " & mat.code & ": " & mat.Name Next i ' 列出所有子类别 Dim subCat As clsCategory Dim j As Long For j = 1 To cat.SubCategories.Count Set subCat = cat.SubCategories(j) Debug.Print " 子类别: " & subCat.categoryName & _ " (物料数:" & subCat.Materials.Count & ")" Next j End If End Sub