下面是我试图在excel中不使用VBA所做的事情的描述:
假设您有一些数据,如下面Sheet1中的图像所示。
如果上面的链接不起作用,下面是sheet1中的数据:
ProductionDate ProductionLine ProductCode ProductName ProductionQuantity
1/5/2017 A 805 AA 100
1/5/2017 B 912 BB 50
13/6/17 A 916 CC 250
15/6/17 B 805 AA 150
23/6/17 B 535 DD 50
2/9/2017 B 916 CC 200
12/9/2017 A 916 CC 200
27/09/17 A 805 AA 200在Sheet2中,我需要按生产线获得每个产品的生产量,也需要该产品的总生产量(所有生产线的生产量之和)。这意味着我需要在sheet2中得到的结果类似于下面的图像。
如果上面的链接不起作用,下面是我在Sheet2中寻找的结果
ProductCode ProductName ProductionLine ProducedQuantityByLine TotalQuanity
535 DD B 50 50
805 AA A 300 450
805 AA B 150 450
912 BB B 50 50
916 CC A 450 650
916 CC B 200 650我真的需要知道怎么做。我需要一些公式来为Sheet2中的每个产品代码自动填充Sheet1。我不能手动输入Productode,因为真实的数量和范围很大(假设1000个产品代码)。
发布于 2017-12-10 16:36:06
下面的代码粘贴在一个模块中,应该可以满足您的需要。不过,也有一些注释和注意事项。
Option Explicit
Sub combineAndCopyData()
Dim wsData As Worksheet, wsResult As Worksheet
Dim i As Long, j As Long
Dim blnRecorded As Boolean, blnAdded As Boolean
Dim lngTotal As Long
Application.ScreenUpdating = False
Application.Calculation = xlManual
Set wsData = ThisWorkbook.Worksheets("Sheet1") 'Substitute Sheet1 for the actual name of your sheet
Set wsResult = ThisWorkbook.Worksheets("Sheet2") 'Subtitue Sheet2 for the actual name of your sheet
For i = 2 To wsData.Cells(2, 1).End(xlDown).Row
blnRecorded = False
blnAdded = False
For j = 1 To wsResult.Cells(wsResult.Rows.Count, 1).End(xlUp).Row + 1
If wsData.Cells(i, 3) = wsResult.Cells(j, 1) And wsData.Cells(i, 2) = wsResult.Cells(j, 3) Then
wsResult.Cells(j, 4) = wsResult.Cells(j, 4) + wsData.Cells(i, 5)
blnRecorded = True
End If
If wsResult.Cells(j, 1) = Empty And Not blnRecorded Then
wsResult.Cells(j, 1) = wsData.Cells(i, 3)
wsResult.Cells(j, 2) = wsData.Cells(i, 4)
wsResult.Cells(j, 3) = wsData.Cells(i, 2)
wsResult.Cells(j, 4) = wsData.Cells(i, 5)
wsResult.Cells(j, 5).Formula = "=SUMIF(A:A," & wsResult.Cells(j, 1).Address & ",D:D)" 'Keep in mind that Excel may require ";" or "," as argument separation.
End If
Next
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub备注
在我最初的回答中,我注意到TotalQuantity on Sheet2没有正确地加起来。上面的代码已经纠正了这个问题。
https://stackoverflow.com/questions/47739541
复制相似问题