我有一个Excel工作簿,我正在建立一个主人。我要做的部分是在我的主表上有饼图。所以我创建了一个工作表,其中包含用于绘制饼图(SortedPie)的选定信息的公式。然后我有另一张将由宏填充的工作表创建饼图(PieSheet)的数据透视表。我的问题是,SortedPie上的信息从1到500的行数发生了变化。在我的宏中,我将SourceData设置为500,但是它会在数据透视表中留下等于所有500行的空白。我尝试将lr=SortedPie.Range("A" & Rows.Count).End(x1Up).Row
与set rng=SortedPie.range("A:X" & lr)
一起使用,然后用于SourceData=:rng.Address(True, True,x1R1C1,True).Refresh
,并且在运行时给出了地址上的编译错误。我应该说我的代码也有Dim lr As Long
和Dim rng As Range
。这就是现在的宏。
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
End Sub
答案 0 :(得分:0)
您可以使用activesheet.usedrange.address获取整个工作表的实际地址,或通过以下方式将范围分配给变量: 将rUsedRange调暗为范围
set rUsedRange = activesheet.usedrange
UsedRange始终提供工作表的活动区域,并在添加其他行或列时随时更新。如果在重新填充之前使用clearcontents命令清除工作表,则在需要时,usedrange将重置为较小的大小。
您还可以通过以下方式获得最后一行:
lRow = activesheet.usedrange.rows.count