Share via


WorksheetBase.PivotTableWizard Method

Definition

Creates a PivotTable object.

public Microsoft.Office.Interop.Excel.PivotTable PivotTableWizard (object sourceType, object sourceData, object tableDestination, object tableName, object rowGrand, object columnGrand, object saveData, object hasAutoFormat, object autoPage, object reserved, object backgroundQuery, object optimizeCache, object pageFieldOrder, object pageFieldWrapCount, object readData, object connection);
member this.PivotTableWizard : obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj * obj -> Microsoft.Office.Interop.Excel.PivotTable
Public Function PivotTableWizard (Optional sourceType As Object, Optional sourceData As Object, Optional tableDestination As Object, Optional tableName As Object, Optional rowGrand As Object, Optional columnGrand As Object, Optional saveData As Object, Optional hasAutoFormat As Object, Optional autoPage As Object, Optional reserved As Object, Optional backgroundQuery As Object, Optional optimizeCache As Object, Optional pageFieldOrder As Object, Optional pageFieldWrapCount As Object, Optional readData As Object, Optional connection As Object) As PivotTable

Parameters

sourceType
Object

An XlPivotTableSourceType value. The source of the report data.

sourceData
Object

The data for the new report. Can be a Range object, an array of ranges, or a text constant that represents the name of another report. For an external database, SourceData is an array of strings containing the SQL query string, where each element is up to 255 characters in length. You should use the Connection argument to specify the ODBC connection string. For compatibility with earlier versions of Excel, SourceData can be a two-element array. The first element is the connection string specifying the ODBC source for the data. The second element is the SQL query string used to get the data. If you specify SourceData, you must also specify SourceType. If the active cell is inside the SourceData range, you must specify TableDestination as well.

tableDestination
Object

A Range object specifying where the report should be placed on the worksheet. If this argument is omitted, the report is placed at the active cell.

tableName
Object

The name of the new report.

rowGrand
Object

true to show grand totals for rows in the report.

columnGrand
Object

true to show grand totals for columns in the report.

saveData
Object

true to save data with the report; false to save only the report definition.

hasAutoFormat
Object

true to have Microsoft Office Excel automatically format the report when it is refreshed or when fields are moved.

autoPage
Object

Valid only if SourceType is xlConsolidation. true to have Excel create a page field for the consolidation. If AutoPage is false, you must create the page field or fields.

reserved
Object

Not used by Excel.

backgroundQuery
Object

true to have Excel perform queries for the report asynchronously (in the background). The default value is false.

optimizeCache
Object

true to optimize the PivotTable cache when it is constructed. The default value is false.

pageFieldOrder
Object

The order in which page fields are added to the PivotTable report’s layout. Can be one of the following XlOrder constants: xlDownThenOver or xlOverThenDown. The default value is xlDownThenOver.

pageFieldWrapCount
Object

The number of page fields in each column or row in the PivotTable report. The default value is 0 (zero).

readData
Object

true to create a PivotTable cache that contains all records from the external database; this cache can be very large. If ReadData is false, you can set some of the fields as server-based page fields before the data is actually read.

connection
Object

A string that contains ODBC settings that allow Excel to connect to an ODBC data source. The connection string has the form "ODBC;<connection string>". This argument overrides any previous setting for the PivotCache object’s Connection property.

Returns

The PivotTable object.

Examples

The following code example uses the PivotTableWizard method to create a PivotTable report based on the contents of the range of cells from A1 to C4 of the current worksheet.

This example is for a document-level customization.

private void CreatePivotTable()
{  
    this.Range["A1"].Value2 = "Date";
    this.Range["A2"].Value2 = "March 1";
    this.Range["A3"].Value2 = "March 8";
    this.Range["A4"].Value2 = "March 15";

    this.Range["B1"].Value2 = "Customer";
    this.Range["B2"].Value2 = "Smith";
    this.Range["B3"].Value2 = "Jones";
    this.Range["B4"].Value2 = "James";

    this.Range["C1"].Value2 = "Sales";
    this.Range["C2"].Value2 = "23";
    this.Range["C3"].Value2 = "17";
    this.Range["C4"].Value2 = "39";

    Excel.PivotTable table1 = this.PivotTableWizard(
        Excel.XlPivotTableSourceType.xlDatabase, this.Range["A1", "C4"],
        "PivotTable1", false, false, true, false, false, false, Excel.XlOrder.xlDownThenOver);
}
Private Sub CreatePivotTable()
    Me.Range("A1").Value2 = "Date"
    Me.Range("A2").Value2 = "March 1"
    Me.Range("A3").Value2 = "March 8"
    Me.Range("A4").Value2 = "March 15"

    Me.Range("B1").Value2 = "Customer"
    Me.Range("B2").Value2 = "Smith"
    Me.Range("B3").Value2 = "Jones"
    Me.Range("B4").Value2 = "James"

    Me.Range("C1").Value2 = "Sales"
    Me.Range("C2").Value2 = "23"
    Me.Range("C3").Value2 = "17"
    Me.Range("C4").Value2 = "39"

    Dim table1 As Excel.PivotTable = _
        Me.PivotTableWizard(Excel.XlPivotTableSourceType.xlDatabase, _
            Me.Range("A1", "C4"), TableName:="PivotTable1", RowGrand:=False, _
            ColumnGrand:=False, SaveData:=True, HasAutoFormat:=False, _
            PageFieldOrder:=Excel.XlOrder.xlDownThenOver)
End Sub

Remarks

This method does not display the PivotTable Wizard. This method is not available for OLE DB data sources. Use the Microsoft.Office.Interop.Excel.Worksheets.Add* method to add a PivotTable cache, and then create a PivotTable report based on the cache.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

Applies to