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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
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.