简体   繁体   English

如何使用VBA将PowerPivot表复制到Excel工作表中?

[英]How to copy a powerpivot table down to an excel sheet with vba?

I need to get my table up in the powerpivot model down to the excel worksheet. 我需要将表在powerpivot模型中放到Excel工作表中。

So far I have tried to use a Recordset but I cant get an ActiveConnection to the powerpivot table. 到目前为止,我已经尝试过使用Recordset,但是无法获得PowerPivot表的ActiveConnection。 Is it possible? 可能吗? Or is there an other better way to do this? 还是有其他更好的方法可以做到这一点?

I use the following code: 我使用以下代码:

Dim name As ADODB.Recordset
Set name = New ADODB.Recordset

With name
       .ActiveConnection = ConnectionName
       .Source = "TableName"
       .LockType = adLockReadOnly
       .CursorType = adOpenForwardOnly
       .Open
End With

But with this piece of code I get an error at .ActiveConnection. 但是,通过这段代码,我在.ActiveConnection处遇到错误。 (Run-time error 3001, it complains about non-allowed connection interval) (运行时错误3001,它抱怨不允许的连接间隔)

This is an example of how to read the records from a named range (assuming 'TableData' is named range). 这是一个如何从命名范围读取记录的示例(假设'TableData'被命名为range)。

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & _
        "Extended Properties=Excel 8.0;"
    .Open
End With

rs.Open "SELECT * FROM [TableName]", cn

Dim r

For Each r In rs.GetRows

    'Do whatever you want per record
    Debug.Print r

Next r

rs.Close
cn.Close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM