Excel数据透视表 – 格式化和分组列与date和另一列

我已经使用了几年的基本的数据透视表,但是我陷入了一些我认为应该相当简单的解决scheme(唉)。

我想以特定的方式格式化我的数据透视表。

例如,只要说我正在使用以下数据:

Client Name Stage Amount Paid Date Paid Client A Start $70,000 1/10/2015 Client A Middle $50,000 1/11/2015 Client A End $30,000 1/12/2015 Client B Start $50,000 5/11/2015 Client B Middle $30,000 5/11/2015 Client B End $50,000 5/12/2015 Client C Start $10,000 10/12/2015 Client C Middle $20,000 20/12/2015 Client C End $30,000 30/12/2015 

我想安排数据透视表,使它看起来像这样:

具有正确格式的表格示例

我几乎可以实现它的唯一方法是看起来像这样:

数据透视表示例 – 格式不正确

我真的需要格式与图片完全一样。

感谢您可能提供的任何帮助。

在excel步骤或VBA代码中的答案将是真棒:)

我认为你不可能有一个你想要的输出结果。 所以我写了一个代码,首先创build一个数据透视表,尽可能靠近你想要的。 然后,其他macros将创build确切的格式表,如您在图片中。

1)然而,你有(它可以很容易地自动化)来replace您的行数据:

Start 1 Start

Middle2

End 3

而你的行数据标题应该在Sheet1并启动单元格A1

主要调用所有的代码:(所有的代码必须在同一个模块中,希望它能帮助你。

 Sub main() Call PivotTable Call FinalTable Call DeleteRow Call FormatTable End Sub 

这是创build数据透视表的第一个代码:

 Sub PivotTable() Dim PTCache As PivotCache Dim PT As PivotTable '1.CREATE DATA STORAGE UNIT Set PTCache = ActiveWorkbook.PivotCaches.Create( _ SourceType:=xlDatabase, _ SourceData:=Range("A1").CurrentRegion) '2. ADD WORKSHEET Worksheets.Add ActiveSheet.Name = "PivotTable1" '3.CREATE PIVOT TABLE N*1 Set PT = ActiveSheet.PivotTables.Add( _ PivotCache:=PTCache, _ TableDestination:=Range("A3")) '4. ENUMERATE PREFERENCES FOR PIVOTE TABLE With PT .PivotFields("Client Name").Orientation = xlRowField .PivotFields("Amount Paid").Orientation = xlRowField .RowAxisLayout xlTabularRow End With 'MODIFYING DATA FIELD CALCULATION With PT.PivotFields("Client Name") .Subtotals(1) = False End With With PT.PivotFields("Date Paid") .Orientation = xlColumnField .Caption = " Date Paid" End With With PT.PivotFields("Stage") .Orientation = xlDataField .Caption = " Stage" .NumberFormat = "[=1]""Start"";[>2]""End"";""Middle""" End With With PT.PivotFields("Amount Paid") .Orientation = xlDataField .Function = xlSum .Caption = " Amount Paid" End With Range("C4").Select Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _ False, True, False, False) PT.DisplayErrorString = False PT.HasAutoFormat = False PT.PivotSelect "", xlDataAndLabel, True Selection.Copy Worksheets.Add ActiveSheet.Name = "FinalTable" Range("A1").PasteSpecial xlPasteValuesAndNumberFormats Rows(1).Delete Columns("B").Delete Columns("I").Delete Columns("H").Delete End Sub 

要格式化:

 Sub FinalTable() Dim Nextcell As Double Dim j As Integer Lastrow = Sheets("FinalTable").Range("A1").SpecialCells(xlCellTypeLastCell).Row i = 3 Do Until i = Lastrow NextProcess i, Nextcell, Lastrow, j For c = 2 To 7 If j = Lastrow Then Exit Do If IsEmpty(Cells(i, c)) Then For j = Nextcell - 1 To i Step -1 If Not IsEmpty(Cells(j, c)) And Not IsEmpty(Cells(j - 1, c)) Then Range(Cells(j, c), Cells(j - 1, c)).Copy Cells(i, c) Range(Cells(j, c), Cells(j - 1, c)).ClearContents Exit For End If If Not IsEmpty(Cells(j, c)) Then Cells(j, c).Copy Cells(i, c) Cells(j, c).ClearContents Exit For End If If Not IsEmpty(Cells(j - 1, c)) Then Cells(j - 1, c).Copy Cells(i, c) Cells(j - 1, c).ClearContents Exit For End If Next j End If Next c StepB = Nextcell - i i = StepB + i Loop i = 2 Do If Application.WorksheetFunction.CountA(Rows(i)) = 0 Then Rows(i).Delete i = i - 1 End If Lastrow = Sheets("FinalTable").Range("A" & Rows.Count).End(xlUp).Row i = 1 + i Loop Until i = Lastrow End Sub 

代码删除最终表中的空行

 Sub DeleteRow() Dim Lastrow As Long Dim i As Integer Lastrow = Sheets("FinalTable").Range("A" & Rows.Count).End(xlUp).Row i = 2 Do If Application.WorksheetFunction.CountA(Rows(i)) = 0 Then Rows(i).Delete i = i - 1 End If Lastrow = Sheets("FinalTable").Range("A" & Rows.Count).End(xlUp).Row i = 1 + i Loop Until i = Lastrow End Sub 

代码把边界放在你的决赛桌上:

 Sub FormatTable() Dim Nextcell As Double Dim j As Integer Lastrow = Sheets("FinalTable").Range("G" & Rows.Count).End(xlUp).Row For i = 2 To Lastrow If Not IsEmpty(Cells(i, 1)) Then If Not IsEmpty(Cells(i + 1, 1)) Then Range(Cells(i, 1), Cells(i, 7)).BorderAround ElseIf Not IsEmpty(Cells(i + 2, 1)) Then NextProcess i, Nextcell, Lastrow, j Range(Cells(i, 1), Cells(Nextcell - 1, 7)).BorderAround Else Range(Cells(i, 1), Cells(Lastrow, 7)).BorderAround End If End If Range(Cells(1, 2), Cells(Lastrow, 3)).BorderAround Range(Cells(1, 4), Cells(Lastrow, 5)).BorderAround Range(Cells(1, 6), Cells(Lastrow, 7)).BorderAround Next i End Sub 

查找下一个客户端名称的子例程:

 Sub NextProcess(ByVal i As Integer, ByRef Nextcell As Double, ByVal Lastrow As Long, ByRef j As Integer) Dim Found As Boolean 'Dim j As Integer Found = False j = i + 1 Do Until Found = True Or Lastrow = j If Not IsEmpty(Range("A" & j).Value) Then Nextcell = Cells(j, 1).Row Found = True End If j = j + 1 Loop End Sub