为指定的数据透视表缓存建立连接。
expression.MakeConnection
expression 必需。该表达式返回一个 PivotCache 对象。
MakeConnection 方法可在缓存断开连接和用户想要重新建立连接之后使用。
如果没有连接缓存,各种对象和方法可能都会返回运行错误。使用该方法可确保在执行其他对象或方法之前已建立一个连接。
如果指定的数据透视表缓存的 MaintainConnection 属性已设置为 False,或指定的数据透视表缓存的 SourceType 属性已设置为 xlExternal,则该方法将导致运行时错误。
注意 Microsoft Excel 可能在会话(对 VBA 程序员未知)期间临时断开一个连接,所以该方法比较有用。
下例决定是否将缓存连接到其源,如有必要,建立一个对源的连接。本示例假定数据透视表缓存位于活动工作表上。
Sub UseMakeConnection()
Dim pvtCache As PivotCache
Set pvtCache = Application.ActiveWorkbook.PivotCaches.Item(1)
' Handle run-time error if external source is not an OLEDB data source.
On Error GoTo Not_OLEDB
' Check connection setting and make connection if necessary.
If pvtCache.IsConnected = True Then
MsgBox "The MakeConnection method is not needed."
Else
pvtCache.MakeConnection
MsgBox "A connection has been made."
End If
Exit Sub
Not_OLEDB:
MsgBox "The data source is not an OLEDB data source"
End Sub