XL2000: How to Obtain the SQL Statement for a PivotTable (213418)



The information in this article applies to:

  • Microsoft Excel 2000

This article was previously published under Q213418

SUMMARY

When you are working with a Microsoft Excel PivotTable, you may want to determine its data source. To do this, use the SourceData property in Microsoft Visual Basic for Applications.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: A Microsoft Excel PivotTable can be based on a result set obtained via Microsoft Query from an external data source. When a PivotTable is created in this way, a Structured Query Language (SQL) SELECT statement is created. The SELECT statement describes which fields to use and from which table to select them. It also specifies any criteria that are applied to the result set.

The SourceData property can be used to return the data source for a PivotTable object. If the data source is an external data source, then the return value for the SourceData property is an array that consists of an SQL connection string, with the remaining elements as the query string broken into 200-character segments. For example, if you create a PivotTable that uses data from the NorthWind data source, and you want to see records from the Orders table with the following criteria
  • The records are from the Employee Id and Shipped Date fields.
  • The Order Date of the records is greater than or equal to June 1, 1996
the SELECT statement for this query would resemble the following:

SELECT Orders.EmployeeID, Orders.ShippedDate
FROM C:\Program Files\Microsoft Office\Office\Samples\Northwind.Orders Orders
WHERE (orders.ShippedDate>={d '1996-06-01 00:00:00'})

To return the SQL connection string and SELECT statement for this sample PivotTable, you could create a macro similar to the following:
Sub GetSourceData()
    Dim SQLString As Variant
    Dim RowCount As Integer
    Dim SQLRange As Range
    Set SQLRange = Range("Sheet1!A1")
    ' Assign the SourceData array to the SQLString variable.
    SQLString = SQLRange.PivotTable.SourceData
    ' Loop through each element of the SQLString array and copy these
    ' elements to Sheet1, starting in cell A1 and going down.
    RowCount = 0
    For Each xElement In SQLString
        ' The first element is the Connection String.
        ' Each additional element is the SELECT Statement
        ' broken in to 200-character text strings.
        Range("A1").Offset(RowCount, 0).Value = xElement
        RowCount = RowCount + 1
    Next
End Sub
				
Note that if the first element, which is the Connection String, is greater than 255 characters, it will be truncated. However, each additional element makes up the SELECT statement and these elements are broken into strings of 200 characters each.

REFERENCES

For more information about the SourceData property, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type sourcedata property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbProgramming kbualink97 KB213418