简体   繁体   English

使用VBA从powerpivot数据获取行数据?

[英]Use VBA to get row data from powerpivot data?

I have imported a table in powerpivot, and were wondering if it's possible to get a row data from that source, by using VBA? 我已经在powerpivot中导入了一个表,并且想知道是否可以通过使用VBA从该源获取行数据?

Lets say I import a table to my powerpivot datamodel from an external source, that looks like below: 可以说我从外部源将表导入到powerpivot数据模型中,如下所示:

col1  col2
 1     tt
 2     tg

As the data is not on any sheet, but in the datamodel. 由于数据不在任何工作表上,而是在数据模型中。 Can I treat this datamodel table like an ordinary table with VBA. 我可以将此数据模型表像使用VBA的普通表一样对待。 eg get a specific row or sum a column? 例如获得特定的行或求和一列?

I have tried searching alot for this, but google results and excel seems to me abit messy. 我曾尝试为此进行大量搜索,但是Google结果和excel在我看来有点凌乱。 I have little experience in VBA, and basically have to idea how/if this is possible. 我对VBA的经验很少,基本上必须知道如何/是否可行。

You can reference the DataModel using CUBE functions, which can be called by VBA if you like. 您可以使用CUBE函数引用DataModel,可以根据需要通过VBA调用该函数。 For examples, see https://powerpivotpro.com/2010/06/using-excel-cube-functions-with-powerpivot/ 有关示例,请参阅https://powerpivotpro.com/2010/06/using-excel-cube-functions-with-powerpivot/

But the question is, why would you want to? 但是问题是,为什么要这么做? To some degree, PowerPivot and the DataModel were designed to let you do complicated things right out of the box with little knowledge of code, instead of having to resort to VBA. 在某种程度上,PowerPivot和DataModel的设计使您无需了解代码就可以立即完成复杂的事情,而不必求助于VBA。 Instead, just write whatever measures or calculated columns you need, and bring them into the worksheet via a PivotTable. 相反,只需编写所需的任何度量或计算列,然后通过数据透视表将它们带入工作表。

Well, you didn't give a lot of details around what you are doing, but I think the script below will be a good place to start. 嗯,您没有提供很多有关您正在做的事情的详细信息,但是我认为下面的脚本将是一个很好的起点。 modify to suit your needs. 修改以适合您的需求。

Sub blah()
Set pt = ActiveSheet.PivotTables(1)
Set pits = pt.PivotFields("???").PivotItems
For Each pit In pits
  If pit.Visible Then 'only acts on visible items
 pit.DataRange.ShowDetail = True
  'pit.name 'contains pivot item name that you could use to refer to a workbook to copy the data on the active sheet to, eg.;
 'ActiveSheet.ListObjects(1).DataBodyRange.Copy Workbooks(pit.Name).Sheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)
 End If
Next pit
End Sub

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

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