level 1
vba复制代码
Sub CheckMaterialAvailability()
Dim wsReq As Worksheet, wsDetail As Worksheet
Dim lastRowReq As Long, lastRowDetail As Long, i As Long, j As Long
Dim reqMaterial As String, detailMaterial As String
Dim detailDate As Date, detailQty As Double
Dim allocatedQty As Double, totalReqQty As Double
' 设置工作表
Set wsReq = ThisWorkbook.Sheets("生产需求")
Set wsDetail = ThisWorkbook.Sheets("来料明细")
' 查找两个工作表的最后一行
lastRowReq = wsReq.Cells(wsReq.Rows.Count, "A").End(xlUp).Row
lastRowDetail = wsDetail.Cells(wsDetail.Rows.Count, "A").End(xlUp).Row
' 遍历生产需求工作表的每一行
For i = 2 To lastRowReq ' 假设第一行是标题行
reqMaterial = wsReq.Cells(i, 1).Value ' 假设料号在第一列
totalReqQty = wsReq.Cells(i, 2).Value ' 假设需求量在第二列
allocatedQty = 0 ' 初始化可分配数量
' 遍历来料明细工作表查找匹配的物料
For j = 2 To lastRowDetail ' 假设第一行是标题行
detailMaterial = wsDetail.Cells(j, 1).Value ' 假设料号在第一列
detailDate = wsDetail.Cells(j, 2).Value ' 假设到厂日期在第二列
detailQty = wsDetail.Cells(j, 3).Value ' 假设来料数量在第三列
' 检查是否找到匹配的物料
If reqMaterial = detailMaterial Then
' 假设物料按到厂日期从旧到新排序,或者只取最新到厂的
' 这里简单累加可分配数量
allocatedQty = allocatedQty + detailQty
' 可以在这里写代码只取特定日期的物料,或者按其他逻辑处理
' 假设这里我们总是取到所有可用的物料
' 停止搜索更多,如果需求已经满足(可选)
If allocatedQty >= totalReqQty Then
Exit For
End If
End If
Next j
' 填写结果到生产需求工作表
wsReq.Cells(i, 5).Value = "最新到厂日期可能需要特别逻辑确定,这里不直接填写"
' 或者假设我们总是取搜索到的第一个到厂日期
' wsReq.Cells(i, 5).Value = detailDate ' 假设这里只取第一个匹配的日期(需要调整逻辑)
wsReq.Cells(i, 6).Value = allocatedQty
If allocatedQty >= totalReqQty Then
wsReq.Cells(i, 7).Value = "OK"
Else
wsReq.Cells(i, 7).Value = "NO"
End If
Next i
MsgBox "物料需求检查完成!"
End Sub
2024年07月28日 08点07分
8