如何在Excel中创建一个始终使用原始数据创建数据透视表的宏?

时间:2017-04-10 13:34:58

标签: excel excel-vba pivot-table excel-pivot vba

我正在尝试自动执行某些工作报告。我希望有一个宏,它将获取工作表中的所有原始数据,并且每次都创建/设计相同的数据透视表。

我已经能够以这种方式创建一个普通的表,通过避免使用Ctrl + shft +向下箭头等绝对值来选择所有数据但是使用数据透视表我遇到了这个错误:

enter image description here

尽可能具体。使用我的宏,我无法:

  1. 创建数据透视表
  2. 重命名工作表
  3. 将值从最大值排序到最小值。
  4. 在我的所有宏中,这三件事会导致它在运行时错误中崩溃。

    是否有修改代码以允许这三件事情起作用?

    这是我的宏代码。我想要完成的三件事是在这个数据透视表宏中。很抱歉,在创建宏时,任何错过点击次数。感谢您提供的任何帮助。

        Sub Create_pivotTable()
    '
    ' Create_pivotTable Macro
    '
    
    '
        Cells.Select
        Sheets.Add
        ActiveWorkbook.Worksheets("Pivot Table").PivotTables("PivotTable6").PivotCache. _
            createPivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
            , DefaultVersion:=xlPivotTableVersion14
        Sheets("Sheet1").Select
        Cells(3, 1).Select
        Sheets("Sheet1").Select
        Sheets("Sheet1").Move Before:=Sheets(5)
        Sheets("Sheet1").Select
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Debit Party Name")
            .Orientation = xlRowField
            .Position = 1
        End With
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Credit Party Name")
            .Orientation = xlRowField
            .Position = 2
        End With
        ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
            "PivotTable1").PivotFields("Original Amount"), "Count of Original Amount", _
            xlCount
        ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
            "PivotTable1").PivotFields("Transaction Date"), "Count of Transaction Date", _
            xlCount
        Sheets("Sheet1").Name = "Piv Tab"
        Rows("1:2").Select
        Range("A2").Activate
        Selection.Delete Shift:=xlUp
        Range("A1").Select
        ActiveSheet.PivotTables("PivotTable1").CompactLayoutRowHeader = _
            "ORIGINATORS | BENEFICIARY'S"
        Range("B1").Select
        With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
            "Count of Original Amount")
            .Caption = "Amount"
            .Function = xlSum
        End With
        Range("C1").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Transaction Date") _
            .Caption = "Count"
        Range("B2").Select
        Range(Selection, Selection.End(xlDown)).Select
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Amount")
            .NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
        End With
        Range("B2").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Debit Party Name"). _
            AutoSort xlDescending, "Amount", ActiveSheet.PivotTables("PivotTable1"). _
            PivotColumnAxis.PivotLines(1), 1
        Range("B4").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Credit Party Name"). _
            AutoSort xlDescending, "Amount", ActiveSheet.PivotTables("PivotTable1"). _
            PivotColumnAxis.PivotLines(1), 1
        Columns("D:D").ColumnWidth = 45.86
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "Analysis"
        Range("B1").Select
        ActiveSheet.PivotTables("PivotTable1").TableStyle2 = "PivotStyleMedium3"
        Range("A1").Select
        Selection.Copy
        Range("D1").Select
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Range("A1").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.Font.Bold = True
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Columns("C:C").Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Range("D7").Select
    End Sub
    

0 个答案:

没有答案