|
楼主 |
发表于 2023-3-24 11:04
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub AllocateData()
' Define the worksheet objects for the total and fill tables
Dim totalTable As Worksheet
Dim fillTable As Worksheet
Dim integratedTable As Worksheet
Set totalTable = ThisWorkbook.Worksheets("完成后表格").Range("总表").Worksheet
Set fillTable = ThisWorkbook.Worksheets("集成配置及排产流程 2023.03.10").Range("填表").Worksheet
Set integratedTable = ThisWorkbook.Worksheets("集成配置及排产流程 2023.03.10").Range("G:I").Worksheet
' Define the data ranges for the total and fill tables
Dim totalData As Range
Dim fillData As Range
Set totalData = totalTable.Range("A1").CurrentRegion
Set fillData = fillTable.Range("A1").CurrentRegion
' Loop through each row in the total data range
For i = 2 To totalData.Rows.Count
' Find the matching row in the fill data range
Set fillRow = fillData.Rows.Find(What:=totalData(i, 1).Value & totalData(i, 6).Value, _
LookIn:=xlValues, LookAt:=xlWhole)
If Not fillRow Is Nothing Then
' Determine the corresponding integrated table row and column
Dim rowHeader As String, columnHeader As String
rowHeader = fillData(fillRow.Row, 4).Value
columnHeader = fillData(fillRow.Row, 1).Value & fillData(fillRow.Row, 2).Value
Set intRange = integratedTable.Range("A1").CurrentRegion
For Each cell In intRange.Cells
If cell.Value = columnHeader And cell.Row = 1 Then
colIndex = cell.Column
Exit For
ElseIf cell.Value Like rowHeader & "*" And cell.Column = 1 Then
rowIndex = cell.Row
Exit For
End If
Next cell
' Determine the integrated table capacity
Dim capacity As Double
If Not rowIndex = 0 And Not colIndex = 0 Then
capacity = integratedTable.Cells(rowIndex, colIndex + 2).Value
End If
' Calculate the quantity to be allocated
Dim quantity As Double, allocatedQty As Double
quantity = totalData(i, 5).Value
allocatedQty = WorksheetFunction.Min(quantity, capacity)
' Allocate the data to the corresponding table
If Not allocatedQty = 0 Then
' Determine the corresponding table letter and row
tableHeader = fillData(fillRow.Row, 2).Value & "-"
tableRow = fillData(fillRow.Row, 5).Value
' Find the matching table sheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "分表 " & tableHeader & "*" Then
Set tableRange = ws.Range("A1").CurrentRegion
Exit For
End If
Next ws
If Not tableRange Is Nothing Then
' Determine the column index in the table range
For Each cell In tableRange.Rows(1).Cells
If cell.Value = tableHeader & tableRow Then
colIndex = cell.Column
Exit For
End If
Next cell
' Find the next available row in the table range and allocate the quantity
For j = 2 To tableRange.Rows.Count
If tableRange(j, 1).Value = "" Then
tableRange(j, colIndex).Value = allocatedQty
Exit For
End If
Next j
End If
End If
End If
Next i
End Sub
这个需要么修改呢。这个可以用吗? |
|