0

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

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

在此处输入图像描述

只是为了尽可能具体。通过我的宏使用,我无法:

  1. 创建数据透视表
  2. 重命名工作表
  3. 将值从大到小排序。

在我所有的宏中,这三件事会导致它在运行时错误中崩溃。

无论如何修改代码以允许这三件事起作用?

这是我的宏代码。我想要完成的三件事都在这个数据透视表宏中。对于宏创建过程中的任何点击失误,我们深表歉意。感谢你们可能提供的任何帮助。

    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
4

0 回答 0