我是VBA的新手,拥有非常基本的知识。我有一个数据透视表,用于计算按注册日期分组的学生数量,列由Brand Ids
组成
我想将一行移动到数据透视表的末尾,并且行数不断变化。我试过下面的代码,但只有当数据透视表中的行数不变时我才能使用它。虽然行数不是恒定的,但有人可以帮我把行移到表的末尾吗?
ActiveSheet.PivotTables("StudentCount").PivotSelect "'En_Date'['(blank)']", _
xlDataAndLabel + xlFirstRow, True
ActiveSheet.PivotTables("StudentCount").PivotFields("En_Date").PivtoItems( _
"(blank)").Position = 20
'In current sheet I need to move to 20 but actually 20 is not constant
ActiveSheet.PivotTables("StudentCOunt").PivotSelect "En_Date", xlButton, True
我也试过下面的代码,但它不起作用:
Dim LS as LONG
LS = ActiveSheet.PivotTable("StudentCount").TableRange2.Rows.Count
ActiveSheet.PivotTables("StudentCount").PivotSelect "'En_Date'['(blank)']", _
xlDataAndLabel + xlFirstRow, True
ActiveSheet.PivotTables("StudentCount").PivotFields("En_Date").PivtoItems( _
"(blank)").Position = "& (LS - 1)"
ActiveSheet.PivotTables("StudentCOunt").PivotSelect "En_Date", xlButton, True
数据透视表:
Count Of Students Date A1 A2 A3 A4 Blank Grand TOtal blank 69 86 23 45 223 01/01/2014 2 25 3 1 31 02/01/2014 1 2 5 8 16 Grand Total 71 112 28 51 262
答案 0 :(得分:1)
您好回答您的问题,您不需要VBA
来执行此操作
您的数据透视表现的原因是因为您的Dates
实际上不是Dates
而是Dates Entered as Text
。
请参见下面重新创建的数据:
以实际dates
重新输入它将解决您的问题
因此,在将Text Dates
更改为实际Dates
后,您的数据透视表会根据您的喜好对您的行进行排序
见下文:
希望我提出的解决方案对您有所帮助。
附加:如果重新输入所有日期不方便,请尝试以下代码:
Dim i As Integer
With ActiveSheet.PivotTables("StudentCount").PivotFields("En Date")
For i = 1 To .PivotItems.Count
If .PivotItems(i) = "(blank)" Then .PivotItems(i).Position = .PivotItems.Count
Next
End With
上面的代码只是遍历所有PivotItems
,查找(blanks)
然后它将它的位置改为最后一个等于.PivotItems.Count
的位置
希望这对你有用。