Sub ListWebQueryPivotTableLinks()
Dim wbA As Workbook, wsN As Worksheet, ws As Worksheet
Dim pt As PivotTable, qt As QueryTable, R As Long, i As Long
Dim vLnkSrc As Variant
Const PROGCREATE As String = "This external " & _
"data range was created " & _
"programmatically and cannot be edited"
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsN = Workbooks.Add(xlWorksheet).Worksheets(1)
wsN.Name = wbA.Name
wsN.Range("A1:E1").Value = Array("Name", "Location", _
"Type", "Connection", "CommandText")
wsN.Range("A1:E1").Font.Bold = True
R = 1
For Each ws In wbA.Worksheets
For Each pt In ws.PivotTables
R = R + 1
With pt.PivotCache
wsN.Cells(R, 1).Value = pt.Name
wsN.Cells(R, 2).Value = ws.Name & "!" & _
pt.TableRange2.Address(False, False)
Select Case .SourceType
Case xlConsolidation
R = R - 1
For i = 1 To UBound(.SourceData)
R = R + 1
wsN.Cells(R, 1).Value = pt.Name
wsN.Cells(R, 2).Value = ws.Name & "!" & _
pt.TableRange2.Address(False, False)
wsN.Cells(R, 3).Value = _
"PivotTable - Consolidation Range"
wsN.Cells(R, 4).Value = "'" & _
.SourceData(i, 1)
wsN.Cells(R, 5).Value = "n/a"
Next
Case xlDatabase
wsN.Cells(R, 3).Value = "PivotTable - Excel List"
wsN.Cells(R, 4).Value = "'" & .SourceData
wsN.Cells(R, 5).Value = "n/a"
Case xlExternal
If .OLAP Then
wsN.Cells(R, 3).Value = "PivotTable - OLAP"
wsN.Cells(R, 4).Value = "'" & .Connection
wsN.Cells(R, 5).Value = .CommandText
ElseIf .QueryType = xlADORecordset Then
wsN.Cells(R, 3).Value = _
"PivotTable - ADO Recordset"
wsN.Cells(R, 4).Value = PROGCREATE
wsN.Cells(R, 5).Value = "'" & .Recordset.Source
Else
wsN.Cells(R, 3).Value = _
"PivotTable - External Data"
wsN.Cells(R, 4).Value = "'" & .Connection
wsN.Cells(R, 5).Value = .CommandText
End If
Case xlScenario
wsN.Cells(R, 3).Value = "PivotTable - Scenario"
wsN.Cells(R, 4).Value = "Based upon a Scenario " & _
"in this workbook"
wsN.Cells(R, 5).Value = "n/a"
End Select
End With
Next
For Each qt In ws.QueryTables
R = R + 1
wsN.Cells(R, 1).Value = qt.Name
wsN.Cells(R, 2).Value = ws.Name & "!" & _
qt.ResultRange.Address(False, False)
Select Case qt.QueryType
Case xlTextImport
wsN.Cells(R, 3).Value = "Text Import"
wsN.Cells(R, 4).Value = "'" & qt.Connection
wsN.Cells(R, 5).Value = "n/a"
Case xlOLEDBQuery
wsN.Cells(R, 3).Value = "Query Table - OLEDB Query"
wsN.Cells(R, 4).Value = "'" & qt.Connection
wsN.Cells(R, 5).Value = "'" & qt.CommandText
Case xlWebQuery
wsN.Cells(R, 3).Value = "Web Query Table"
wsN.Cells(R, 4).Value = "'" & qt.Connection
wsN.Cells(R, 5).Value = "n/a"
Case xlADORecordset
wsN.Cells(R, 3).Value = "Query Table - ADO Recordset"
wsN.Cells(R, 4).Value = PROGCREATE
wsN.Cells(R, 5).Value = "'" & qt.Recordset.Source
Case xlDAORecordset
wsN.Cells(R, 3).Value = "Query Table - DAO Recordset"
On Error Resume Next
wsN.Cells(R, 4).Value = "'" & qt.Recordset.Parent.Name
If Err.Number <> 0 Then
wsN.Cells(R, 4).Value = PROGCREATE
Err.Clear
End If
wsN.Cells(R, 5).Value = "'" & qt.Recordset.Name
If Err.Number <> 0 Then
wsN.Cells(R, 5).Value = PROGCREATE
Err.Clear
End If
On Error GoTo errHandler
Case xlODBCQuery
wsN.Cells(R, 3).Value = "Query Table"
wsN.Cells(R, 4).Value = "'" & qt.Connection
wsN.Cells(R, 5).Value = qt.CommandText
End Select
Next
Next
vLnkSrc = wbA.LinkSources
If Not IsEmpty(vLnkSrc) Then
For i = 1 To UBound(vLnkSrc)
R = R + 1
wsN.Cells(R, 1).Value = "n/a"
wsN.Cells(R, 2).Value = "n/a"
wsN.Cells(R, 3).Value = "Link Source (Edit | Links)"
wsN.Cells(R, 4).Value = vLnkSrc(i)
Next
End If
wsN.Cells.WrapText = False
wsN.Columns.AutoFit
wsN.UsedRange.AutoFilter
Exit Sub
errHandler:
MsgBox "An error has occurred." & vbCr & Err.Number & _
vbCr & Err.Description
Resume Next
End Sub