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

如何在excel中创建始终使用原始数据创建透视表的宏?,excel,pivot-table,excel-pivot,vba,Excel,Pivot Table,Excel Pivot,Vba,我正在尝试自动化我的一些工作报告。我希望有一个宏,它将获取工作表中的所有原始数据,并每次创建/设计相同的透视表 我已经能够以这种方式创建一个普通表,方法是避免使用绝对值,例如Ctrl+shft+向下箭头来选择所有数据,但使用透视表时,我遇到了以下错误: 只是尽可能具体。使用宏时,我无法: 创建透视表 重命名工作表 将值从最大值排序到最小值 在我所有的宏中,这三种情况都会导致它在运行时错误中崩溃 是否需要修改代码以允许这三件事工作 这是我的宏代码。我想完成的三件事是在这个透视表宏中。很抱歉,在创

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

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

只是尽可能具体。使用宏时,我无法:

  • 创建透视表
  • 重命名工作表
  • 将值从最大值排序到最小值 在我所有的宏中,这三种情况都会导致它在运行时错误中崩溃

    是否需要修改代码以允许这三件事工作

    这是我的宏代码。我想完成的三件事是在这个透视表宏中。很抱歉,在创建宏过程中没有单击。谢谢你们的帮助

        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
    

    在哪一行出现此错误?当我运行宏时,宏完成的唯一任务是创建新工作表。这张纸是空白的。那么第四行呢?