1.Power Pivot为com加载项,直接用application.addin对象是不能将Power Pivot加载到EXCEL 功能选项卡上的
2.所以我们要首先获取com加载项的description,以获得power pivot的index
代码如下:
Sub 获取com加载项的descrition()
Dim objAddIn As Object
With Worksheets("sheet1")
.Rows(1).Font.Bold = True
.Range("a1").Value = _
Array("description")
For i = 1 To Application.COMAddIns.Count
Set objAddIn = Application.COMAddIns.Item(i)
.Cells(i + 1, 1) = objAddIn.Description
Next
.Range("a1").CurrentRegion.Columns.AutoFit
End With
End Sub
得到的结果如下所示:

以下代码是自动加载Power Pivot的代码:
Sub 加载powerpivot组件()
If Application.COMAddIns(4).Connect = False Then
'这里item为4,是因为上表中 Microsoft power pivot for EXCEL 顺序排在第四
Application.COMAddIns(4).Connect = True
Else
MsgBox "您已经加载power pivot组件,请放心使用"
End If
End Sub