通过VBA在Excel数据透视表中显示具有重复数量的重复行
我有一个物料清单(BOM)表,其中有不同功能的重复项。我有一个VBA代码来自动创建这个透视表,看看函数列的总值是多少通过VBA在Excel数据透视表中显示具有重复数量的重复行,vba,excel,pivot-table,Vba,Excel,Pivot Table,我有一个物料清单(BOM)表,其中有不同功能的重复项。我有一个VBA代码来自动创建这个透视表,看看函数列的总值是多少 A B C D Description Quantity Cost($) Function 1 Pr. Vessel_A 1
A B C D
Description Quantity Cost($) Function
1 Pr. Vessel_A 1 320 Pre Treat
2 Pr. Vessel_A 1 320 Post Treat
3 Pr. Vessel_A 1 320 Primary RO
4 Pr. Vessel_A 1 320 Pre Treat
5 Pr. Vessel_A 20 6400 Secondary RO
6 Membrane_00B 5 505 Pre Treat
7 Membrane_00B 5 505 Primary RO
.
.
.
所以我想看到的是:
Function Quantity Cost($) Description
1 Pre Treat 1 320 Pr. Vessel_A
2 Pre Treat 1 320 Pr. Vessel_A
3 Pre Treat 5 505 Membrane_00B
4 Total Pre Treat 1145
5 Primary RO 5 505 Membrane_00B
6 Primary RO 20 6400 Pr. Vessel_A
7 Total Primary RO 6905
8 Post Treat 1 320 Pr. Vessel_A
9 Total Post Treat 320
10 Secondary RO 1 320 Pr. Vessel_A
11 Total Secondary RO 320
.
.
.
我使用此代码很长时间了,但今天我意识到,如果同一函数中的项目描述和数量相同,则透视表仅显示其中一个。
在这个例子中,我只看到预处理的1数量
功能,但成本为2个数量。好吧,总成本是正确的,但数量不是。与此相反,我需要看到两条不同的线,以便正确地看到数量
这就是我现在看到的:
Function Quantity Cost($) Description
1 Pre Treat 1 640 Pr. Vessel_A
2 Pre Treat 5 505 Membrane_00B
3 Total Pre Treat 1145
我一直试图自己纠正这一点,但没能做到。也没有在网上找到确切的解决方案。那么,我应该如何编辑代码,以便在透视表中也看到重复描述行和重复数量?
这是我的代码:
Sub CreateProcesSectList()
'Create worksheet "ProcessSectionsList" if it doesn't exist. And then Create a Process Sections Pivot Table
Dim Baslik1, Baslik2, Baslik3, Baslik4, Baslik5 As String
Baslik1 = Sheet5.Range("F2").Value 'Items' Descriptions
Baslik2 = Sheet5.Range("H2").Value 'Process Sections
Baslik3 = Sheet5.Range("I2").Value 'Quantity
Baslik4 = Sheet5.Range("K2").Value 'Unit cost
Baslik5 = Sheet5.Range("M2").Value 'Total Cost
Application.ScreenUpdating = False
CreateSheetIf ("ProcessSectionsList")
Sheets("ProcessSectionsList").Select
Columns("A:AK").Select
Range("A1").Activate
Selection.Delete Shift:=xlToLeft
Sheet5.Select
Application.Goto Reference:="R100000C6"
Selection.End(xlUp).Select
ActiveCell.CurrentRegion.Select
Set tblo = ActiveCell.CurrentRegion
tblo.Offset(1, 0).Resize(tblo.Rows.Count - 1, _
tblo.Columns.Count).Select
'Sheet5.Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"R2C2:R388C22", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="ProcessSectionsList!R1:R1048576", TableName:="ProcessSectionsPivotTable", _
DefaultVersion:=xlPivotTableVersion14
Range("a1").Select
Sheets("ProcessSectionsList").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik2)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik1)
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik3)
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik4)
.Orientation = xlRowField
.Position = 4
End With
ActiveSheet.PivotTables("ProcessSectionsPivotTable").AddDataField ActiveSheet.PivotTables( _
"ProcessSectionsPivotTable").PivotFields(Baslik5), "Count of Total Cost", xlCount
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik2)
.LayoutForm = xlTabular
.RepeatLabels = True
End With
ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik1). _
Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
False, False)
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik1)
.LayoutForm = xlTabular
.RepeatLabels = True
End With
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik3)
.LayoutForm = xlTabular
.RepeatLabels = True
.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
False, False)
End With
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik4)
.LayoutForm = xlTabular
.RepeatLabels = True
.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
False, False)
End With
Range("C223").Select
ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields("Count of Total Cost"). _
Function = xlSum
Range("A1").Select
With ActiveSheet.PivotTables("ProcessSectionsPivotTable").PivotFields(Baslik2)
.PivotFilters.Add Type:=xlCaptionDoesNotBeginWith, Value1:="0"
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("ProcessSectionsPivotTable")
.TableStyle2 = "PivotStyleLight20"
.ShowDrillIndicators = False
End With
Range("a1").Select
ActiveSheet.PivotTables("ProcessSectionsPivotTable").CompactLayoutRowHeader = Baslik2
Columns("d:e").Select
Selection.Style = "Currency"
Columns("B:B").ColumnWidth = 54.14
Range("a1").Select
ActiveWindow.ScrollRow = 1
Application.ScreenUpdating = True
End Sub
只要我有Baslik1和Baslik3(数量和描述)的“.RepeatLabels=True”,缺少什么?我通过向表中添加另一个数据列解决了这个问题
在这种情况下,我的表格需要重复我的所有项目,只要它们新添加的列中的值不同。是
CreateSheetIf
other Sub?@MarkFitzgerald是,但问题不在于该Sub,CreateSheetIf
仅在ProcessSectionsList工作表不存在时才创建它。我想您需要一些东西来区分重复的数据行-可能是另一列“唯一ID”,每个数据行都有序列号。@MarkFitzgerald,当我录制宏并手动执行这些步骤以获取代码时,它看起来非常相似,所以没有必要再添加一列。我在这段代码中遗漏了一些东西,但还没有找到。@MarkFitzgerald我刚刚编辑了这个问题。你能检查一下吗。我意识到我的行号不正确,这就是总数不同的原因。但是在我更正了行号之后,现在同样的问题还在继续,除了我看到了正确的总数。因此,我的数据透视表可以识别重复的行并对其进行计数,但不会显示所有行,而只显示其中一行的数量。