在添加的工作表中放置数据透视表

时间:2016-05-09 17:58:26

标签: excel-vba vba excel

我有vba代码,用于提取数据,然后从数据中创建数据透视表。现在发生的事情是每次按下按钮创建数据透视表时,它都会被放入一个具有不同数字的新工作表中。我想要发生的是我创建的新工作表中的数据透视表。我添加了下面编写的代码。提前谢谢。

On Error Resume Next
Set pc = src.PivotCaches.Create(xlDatabase, Range("A1").CurrentRegion)

Set wsNew = Sheets.Add
wsNew.Name = "DAM-AWW Pivot"

Set objtable = ws.PivotTableWizard(, , , , False)
objtable.Name = "DAM-AWW Pivot"

1 个答案:

答案 0 :(得分:0)

当我使用VBA创建数据透视表时,我没有使用向导。我发现通过两个步骤将PT准确地放在我想要的位置更灵活:创建PivotCache然后创建PivotTable

以下是我一直用来更新或创建新PivotTable的功能剪切/粘贴:

Private Sub UpdatePivotTable(ptwb As Workbook, ptName As String, ptRange As Range)
    On Error GoTo Err_UpdatePivotTable

    '--- refreshes the data in the given pivot table for the (potentially)
    '    expanded data range; creates the pivot table if it doesn't exist
    Dim thisPivot As PivotTable
    Dim ptCache As PivotCache
    Dim ptSheet As Worksheet
    Dim ptField As PivotField
    Dim ptshname As String

    '--- make sure the destination workbook and sheet are correctly
    '    set up and ready
    ptshname = ptName & " Pivot"
    Set ptSheet = ptwb.Sheets(ptshname)

    '--- does the pivot table even exist?
    On Error Resume Next
    Set thisPivot = ptSheet.PivotTables(ptName)
    On Error GoTo Err_UpdatePivotTable

    If thisPivot Is Nothing Then
        Set ptCache = ptwb.PivotCaches.Create(SourceType:=xlDatabase, _
                                              SourceData:=ptRange, _
                                              version:=xlPivotTableVersion12)
        Set thisPivot = ptCache.CreatePivotTable(TableDestination:=ptSheet.Range("B9"), _
                                                 TableName:=ptName, _
                                                 DefaultVersion:=xlPivotTableVersion12)
        With thisPivot
            Set ptField = .PivotFields("ProjectID")
            ptField.Orientation = xlRowField
            ptField.Position = 1
            Set ptField = .PivotFields("ProjectName")
            ptField.Orientation = xlRowField
            ptField.Position = 2
            Set ptField = .PivotFields("LaborMonth")
            ptField.Orientation = xlColumnField
            ptField.Position = 1
            Set ptField = .AddDataField(.PivotFields("Hours"), "Sum of Hours", xlSum)
            .InGridDropZones = True
            .RowAxisLayout xlTabularRow
            For Each ptField In .PivotFields
                ptField.Subtotals(1) = True
                ptField.Subtotals(1) = False
            Next ptField
            .DataBodyRange.NumberFormat = "0.00;;-"
            .DataBodyRange.HorizontalAlignment = xlCenter
            .ColumnRange.HorizontalAlignment = xlCenter
            .TableStyle2 = "PivotStyleMedium9"
        End With
    Else
        thisPivot.ChangePivotCache ptwb.PivotCaches.Create(SourceType:=xlDatabase, _
                                                           SourceData:=ptRange, _
                                                           version:=xlPivotTableVersion12)
        thisPivot.PivotCache.Refresh
    End If
    thisPivot.PivotFields("LaborMonth").AutoSort xlAscending, "LaborMonth"

Exit_UpdatePivotTable:
    On Error GoTo 0
    Exit Sub

Err_UpdatePivotTable:
    MsgBox "Error " & Err.Number & " (" & Err.Description & _
           ") in procedure UpdatePivotTable of " & _
           "Module CostpointLabor", vbOKOnly
    Resume Exit_UpdatePivotTable
End Sub

显然,大多数情况都是用来根据您的具体需求量身定制的一个例子。