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

时间:2017-11-19 12:19:02

标签: excel vba excel-vba

我在powerpivot中导入了一个表,并想知道是否可以通过使用VBA从该源获取行数据?

让我说我从外部源导入一个表到我的powerpivot数据模型,如下所示:

col1  col2
 1     tt
 2     tg

由于数据不在任何工作表上,而是在数据模型中。我可以将此数据模型表视为具有VBA的普通表。例如得到一个特定的行或总和一列?

我试过很多搜索,但google结果和excel似乎让我感到很乱。我在VBA方面经验不足,基本上不得不知道如何做到这一点。

2 个答案:

答案 0 :(得分:0)

您可以使用CUBE函数引用DataModel,如果您愿意,可以通过VBA调用它们。有关示例,请参阅https://powerpivotpro.com/2010/06/using-excel-cube-functions-with-powerpivot/

但问题是,你为什么要这样做?在某种程度上,PowerPivot和DataModel旨在让您只需很少的代码知识即可开箱即用,而不必诉诸VBA。相反,只需编写您需要的任何度量或计算列,并通过数据透视表将它们带入工作表。

答案 1 :(得分:0)

嗯,你没有提供很多有关你正在做什么的细节,但我认为下面的脚本将是一个很好的起点。修改以满足您的需求。

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
相关问题