首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何将宏中的SourceData设置为只计算所需行数?

如何将宏中的SourceData设置为只计算所需行数?
EN

Stack Overflow用户
提问于 2015-04-07 13:38:48
回答 1查看 305关注 0票数 0

我有一个Excel工作簿,我正在构建作为一名硕士。我要做的一部分就是在我的主表上有饼图。因此,我创建了一个表,其中有公式提取选定的信息,用于饼图(SortedPie)。然后,我有另一个表,它将通过宏来填充,为饼图(PieSheet)创建枢轴表。我的问题是,关于SortedPie的信息从1行更改到500行。在我的宏中,我将SourceData设置为500,但它在透视表中留下空白,以等于所有500行。我尝试过在lr=SortedPie.Range("A" & Rows.Count).End(xlUp).Rowset rng=SortedPie.range("A:X" & lr)一起使用,然后对于SourceData=:rng.Address(True, True,xlR1C1,True).Refresh,当运行时给出地址上的编译错误。我应该说我的代码也有Dim lr As LongDim rng As Range。这就是现在的宏。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "SortedPie!R1C1:R500C5", Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="PieSheet!R1C1", TableName:="PivotTable2", _
    DefaultVersion:=xlPivotTableVersion14
Sheets("PieSheet").Select
cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Credit Score")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").PivotFields("CustomerAccountID"), "Count of CustomerAccountID", _
    xlCount
Sheets("SortedPie").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "SortedPie!R1C1:R185C5", Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="PieSheet!R10C1", TableName:="PivotTable3", _
    DefaultVersion:=xlPivotTableVersion14
Sheets("PieSheet").Select
cells(10, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("PanelType")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
    "PivotTable3").PivotFields("CustomerAccountID"), "Count of CustomerAccountID", _
    xlCount
Sheets("SortedPie").Select
ActiveWorkbook.Worksheets("PieSheet").PivotTables("PivotTable3").PivotCache. _
    CreatePivotTable TableDestination:="PieSheet!R20C1", TableName:= _
    "PivotTable4", DefaultVersion:=xlPivotTableVersion14
Sheets("PieSheet").Select
cells(20, 1).Select
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Status")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables( _
    "PivotTable4").PivotFields("CustomerAccountID"), "Count of CustomerAccountID", _
    xlCount
Sheets("SortedPie").Select
ActiveWorkbook.Worksheets("PieSheet").PivotTables("PivotTable4").PivotCache. _
    CreatePivotTable TableDestination:="PieSheet!R30C1", TableName:= _
    "PivotTable5", DefaultVersion:=xlPivotTableVersion14
Sheets("PieSheet").Select
cells(30, 1).Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("BillingMethod")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
    "PivotTable5").PivotFields("CustomerAccountID"), "Count of CustomerAccountID", _
    xlCount

结束子对象

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-07 15:16:23

您可以使用activesheet.usedrange.address获取整个工作表的实际地址,也可以通过: Dim rUsedRange作为范围将范围分配给变量。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
set rUsedRange = activesheet.usedrange

UsedRange总是给出工作表的活动区域,并随时更新添加的其他行或列。如果在重新填充之前使用“清除内容”命令清除工作表,则使用范围将在需要时重置为较小的大小。

您还可以通过以下方法获得底部的行:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
lRow = activesheet.usedrange.rows.count
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29501808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文