一个工作表提取另一个工作表格数据的VB编程?

请教如何将图一物料名称按日期汇总数量后填入图二对应的日期&物料名称的单元格里面?谢谢!!!

在 Excel 中,您可以使用 VBA 编程来根据一个工作表的数据提取并汇总到另一个工作表。以下是一个简单的示例,从 Sheet1 汇总物料名称的数量,并将结果填入 Sheet2 中的对应日期和物料名称单元格:
Sub SummarizeData()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastRowWs1 As Long, lastRowWs2 As Long
Dim i As Long, j As Long
Dim currentDate As Date, currentMaterial As String
Dim quantitySum As Long

Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

lastRowWs1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lastRowWs2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row

' Loop through each row in Sheet2
For i = 2 To lastRowWs2
currentDate = ws2.Cells(i, 1)
currentMaterial = ws2.Cells(i, 2)
quantitySum = 0

' Loop through each row in Sheet1
For j = 2 To lastRowWs1
If ws1.Cells(j, 1) = currentDate And ws1.Cells(j, 2) = currentMaterial Then
quantitySum = quantitySum + ws1.Cells(j, 4)
End If
Next j

' Fill the summarized quantity in Sheet2
ws2.Cells(i, 3) = quantitySum
Next i
End Sub
请注意,这个示例代码中的列索引可能需要根据您的实际数据表格进行调整。在此示例中,我们假设 Sheet1 中的日期在第一列,物料名称在第二列,数量在第五列。同时,我们假设 Sheet2 中的日期在第一列,物料名称在第二列,汇总数量在第三列。
要运行此代码,请按 Alt + F11 打开 Visual Basic for Applications (VBA) 编辑器。然后,单击“插入”>“模块”,并将代码粘贴到新模块窗口中。在 Excel 中按 Alt + F8,选择 SummarizeData,然后单击“运行”。这将根据 Sheet1 的数据填充 Sheet2 中的汇总数量。
温馨提示:答案为网友推荐,仅供参考
相似回答