2017-03-07 51 views
0

我有一個物料清單(BOM)表,其中有重複的項目用於不同的功能。我有一個VBA代碼來自動創建這個數據透視表,以查看Funtion列的總值。通過VBA在Excel Excel數據透視表中顯示重複的行

   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  
    . 
    . 
    . 

我使用這個代碼很長一段時間,但我意識到,如果項目說明和數量都相同在相同的功能,然後數據透視表只顯示我其中之一。

而且在這個例子中,我只看到數量鐠。預處理 功能,但成本爲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 

只要我有「.RepeatLabels =真」爲Baslik1和Baslik3(數量和說明)缺什麼?

+0

'CreateSheetIf'是否是另一個Sub? –

+0

@MarkFitzgerald是的,但問題不在於該子,'CreateSheetIf'只創建一個ProcessSectionsList表,如果它不存在。 – Mertinc

+0

我想你需要一些東西來區分重複的數據行 - 可能是每個數據行的序列號都是另一列「唯一ID」。 –

回答

0

我通過在表中添加另一個數據列來解決此問題。

在這種情況下,只要新添加的列中的值不同,我的表就需要重複所有項目。